---恢复内容开始---
用@AutoWired和@Resource自动装配Bean
1.@AutoWired和@Resource介绍
Spring的主配置文件如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="liusheng.springboot.Spring"/>
<!-- 学生--> <bean name="student" class="ls.entity.Student"> <property name="student_ID" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="19"></property> </bean>
<!-- 用户-->
<bean name="user" class="ls.entity.User"> <property name="age" value="10"></property> <property name="name" value="李四"></property> </bean> </beans>
@AutoWired
@AutoWired只有一个属性:required boolean 类型 该值默认是true ,
它会从你的配置文件中寻找类型一致或者类型存在继承,实现的关系的bean
如果required=true,找不到就报异常 ,required=false 找不到就不注入了
@AutoWired 的定义
从定义上看这个注解可以作用在构造函数上,字段上,方法上,注解上(注解上我不知道怎么用)
实体类Student
package ls.entity; /** * 学生类 * @author liusheng */ public class Student { /** * 学号 */ private String Student_ID; /** * 名字 */ private String name; /** * 年龄 */ private int age; public String getStudent_ID() { return Student_ID; } public void setStudent_ID(String student_ID) { Student_ID = student_ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "Student [Student_ID=" + Student_ID + ", name=" + name + ", age=" + age + "]"; } }
实体类 User
package ls.entity; /** * User class * @author liusheng */ public class User { /** * name */ private String name; /** * age */ private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
字段上
package liusheng.springboot.Spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ls.entity.Student; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AotuWiredTest { @Autowired Student student; @Test public void test1() throws Exception { System.out.println(student); } }
因为在字段上,所以不需要set方法,通过反射对属性直接赋值
通过方法赋值(注意:这个方法不一定是setStudent(Student student) ,还可以是setaa(Student student);)
package liusheng.springboot.Spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ls.entity.Student; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AotuWiredTest { Student student; @Test public void test1() throws Exception { System.out.println(student); } @Autowired public void setStudent(Student student) { this.student = student; } }
通过构造方法赋值(注意:要保留无参构造函数)
package liusheng.springboot.Spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import ls.entity.Student; @Component public class ConstuctorWired { private Student student; public Student getStudent() { return student; } public ConstuctorWired() { } @Autowired public ConstuctorWired(Student student) { this.student = student; } public String toString() { return "constuctorWired [student=" + student + "]"; } public void setStudent(Student student) { this.student = student; } }
package liusheng.springboot.Spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ls.entity.Student; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AotuWiredTest { @Autowired ConstuctorWired c; @Test public void test1() throws Exception { System.out.println(c); } public AotuWiredTest() { } }
@Resource
该注解可以用在方法上和字段上
如果不写name属性那么按照类型注入,如果指定的name 属性那么就按name注入
package liusheng.springboot.Spring; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ls.entity.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class ResourceAutoWired { @Resource() private User user; private User user1; @Resource(name="user") private User user2; @Resource(name="user") public void setUser1(User user1) { this.user1 = user1; } @Test public void test() throws Exception { System.out.println("有name属性在方法上"+user1); System.out.println("有name属性在字段上"+user2); System.out.println("没有name属性"+user); } }
console输出:
总结:这两个注解很常用,要多理解理解。我是初学者,今天是总结的,博客可能很烂,有错误的地方请大牛指点迷津。。。