Spring使用筆記(一)Bean裝配
阿新 • • 發佈:2018-12-07
Bean裝配
Spring提供了3種裝配機制:
1)隱式的Bean發現機制和自動裝配
2)在Java中進行顯示裝配
3)在XML中進行顯示裝配
一)自動化裝配
1.指定某類為元件類:
@Component public class Dog { private String name = "Jerry"; //省略getter/setter }
public interface Person { void introduce(); }
//表明該類會作為元件,告知Spring為該類建立bean //該元件的預設id為:student @Component//@Component("aStudent") 為該bean指定id:aStudent //當然也可以使用Java依賴注入規範: //@Named("aStudent") public class Student implements Person{ @Autowired private Dog dog; private String name = "Tom"; private int age = 6; private String school = "Happy School"; public Student() { } public Student(String name, intage, String school) { this.name = name; this.age = age; this.school = school; } public void introduce() { System.out.println("My name is " + name + ", I'm " + age + " years old. I'm from " + school + " !"); System.out.println("I have dog named " + dog.getName()); }//省略getter/setter }
2.建立配置類啟用元件掃描
1)通過Java配置:
@Configuration //@ComponentScan 啟用元件掃描功能,此時是掃描該類所在包下的所有元件類 @ComponentScan("beans") //設定要掃描的包 //@ComponentScan(basePackages = "beans") 同上 //@ComponentScan(basePackages = {"a", "b", "c"}) 掃描多個包 //@ComponentScan(basePackageClasses = {a.class, b.class, c.class}) 掃描class所在包 public class PersonConfig { }
2)通過xml:
<?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.xsd"> <Context:component-scan base-package="beans"/> </beans>
3.測試:
//該註解建立了Spring應用上下文 @RunWith(SpringJUnit4ClassRunner.class) //載入配置 @ContextConfiguration(classes = PersonConfig.class) public class PersonTest { //@Autowired(required = false) 設定為false沒有匹配bean時不丟擲異常 @Autowired //當然該註解也可用於方法 Person student; @Test public void say() { student.introduce(); //My name is Tom, I'm 6 years old. I'm from Happy School ! //I have dog named Jerry } }
二)通過Java裝配
public class Father implements Person{ private Student student; public Father(Student student) { this.student = student; } public void introduce() { System.out.println("I'm " + student.getName() + "'s father."); } }
@Configuration public class PersonConfig { @Bean public Dog dog() { Dog dog = new Dog(); dog.setName("Flex"); return dog; } @Bean //告訴Spring返回的物件註冊為Spring應用上下文的bean //@Bean(name = "aName") 修改預設名字 public Student getStudent(){ Student student = new Student("Jimmy", 22, "Sad School"); student.setDog(dog()); return student; } //此處註解會攔截getStudent()方法,傳入Spring建立的單例 @Bean public Father getFather() { return new Father((Student) getStudent()); } //通過這種方法引用其他bean是最佳方式 //因為它不要求將person宣告到同一配置檔案中 public Person father(Person person) { return new Father((Student) person); } }
@Autowired Student student; @Autowired Father father; @Test public void say() { student.introduce(); father.introduce(); /*My name is Jimmy, I'm 22 years old. I'm from Sad School ! I have dog named Flex I'm Jimmy's father.*/ }
三)通過XML裝配
PersonTest-contxt.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="beans.Book" id="book1"> <property name="bookName" value="YellowBook"/> </bean> <bean class="beans.Book" id="book2"> <property name="bookName" value="GreenBook"/> </bean> <bean class="beans.Dog" id="dog"> <!--屬性注入必須提供相應的屬性set方法--> <property name="name" value="小白"/> </bean> <bean class="beans.Student" id="student"> <property name="dog" ref="dog"/> <constructor-arg value="Harry"/> <constructor-arg value="28" /> <constructor-arg value="Killer School"/> <property name="books"> <list> <ref bean="book1"/> <ref bean="book2"/> </list> </property> </bean> <bean class="beans.Father" id="father"> <constructor-arg ref="student" /> </bean> </beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration // @ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
public class PersonTest {
@Autowired
Student student;
@Autowired
Father father;
@Test
public void say() {
student.introduce();
father.introduce();
/* My name is Harry, I'm 28 years old. I'm from Killer School !
I have dog named 小白
I have 2 books.
I'm Harry's father.*/
}
}
四)匯入混合配置
1.在JavaConfig匯入其他JavaConfig
@Import(AnotherConfig.class)
@Import({One.class, Tow.class})
2.在JavaConfig中匯入xml配置
@ImportResource("classpath: one.xml")
3.在xml中匯入其他xml
<import resoune="one.xml">
注意並沒有XML能匯入JavaConfig