---恢復內容開始---
用@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輸出:
總結:這兩個注解很常用,要多理解理解。我是初學者,今天是總結的,博客可能很爛,有錯誤的地方請大牛指點迷津。。。