文明网站机制建设,围场网站建设,正能量免费软件,百度推广点击一次多少钱Resource在bean注入的时候使用#xff0c;Resource所属包其实不是spring,而是javax.annotation.Resource#xff0c;只不过spring支持该注解Resource里有name,lookup,type,authenticationType,shareable,mappedName,description这几个属性具体看源码结构截图0、可以直接在要注…Resource在bean注入的时候使用Resource所属包其实不是spring,而是javax.annotation.Resource只不过spring支持该注解Resource里有name,lookup,type,authenticationType,shareable,mappedName,description这几个属性具体看源码结构截图0、可以直接在要注入的属性上这样写Resourceprivate User user;不管配置文件里有没有写id或name,都能识别到默认应该是根据类class来识别的。1、name当spring的xml配置文件中的bean配置了name或id时它都可以识别到也就是如果[email protected](nameuser_id)[email protected](nameuser_name)都可以2、lookup3、typeClass type() default java.lang.Object.class;这个是源码默认值是Object.class那就一目了然了无需多说了4、authenticationTypeAuthenticationType authenticationType() default AuthenticationType.CONTAINER;这个是源码AuthenticationType这是个枚举类有两个属性CONTAINER,APPLICATION。暂时没搞懂这俩有啥区别有知道的麻烦告知5、shareable指示此组件和其他组件之间是否可以共享该资源。这可以指定代表任何支持类型的连接工厂的资源并且不能为其他类型的资源指定。它是一个布尔型默认是true6、mappedName7、description当然spring的注入不光是可以属性注入也可以set方法和构造函数注入也就是说Resourcepublic void setUser(User user) {this.user user;}Autowired是属于spring的注解它所在的包org.springframework.beans.factory.annotation它是按byType注入的默认是要依赖的对象必须存在看源码就可以理解boolean required() default true;可以看到默认值是true,如果需要允许依赖对象是null,[email protected](requiredfalse)就可以了。[email protected][email protected]AutowiredQualifier(user)private User user;