Spring的bean定义方式
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="dog" class="cc.anti.Dog"></bean>
<bean id ="people" class="cc.anti.People" >
<property name="dog" ref="dog"></property>
</bean>
</beans>
package cc.anti;
public class Dog {
@Override
public String toString() {
return "dog";
}
}
package cc.anti;
import cc.anti.Cat;
import cc.anti.Dog;
public class People {
private Dog dog;
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "people+" + cat.toString() +"+" +dog.toString();
}
}
package cc.anti;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void MyTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people",Cat.class);
System.out.printf(cat.toString());
}
}
javaConfig
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启自动扫描 -->
<context:component-scan base-package="cc.anti.javaConfigTest"/>
</beans>
package cc.anti.javaConfigTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //等价于 <beans> </beans>
public class House {
@Bean //等价于 <bean> <bean>
public Door door(){
return new Door();
}
@Override
public String toString() {
return "house";
}
}
package cc.anti.javaConfigTest;
public class Door {
@Override
public String toString() {
return "door";
}
}
public void MyJavaConfigTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans-javaConfig.xml");
House house = context.getBean("house", House.class);
System.out.printf(house.toString());
Door door = context.getBean("door", Door.class);
System.out.printf(door.toString());
}
注解
@Component : @Service、@Controller等
装配方式
非自动装配
见 Spring的bean定义方式-xml的例子;
自动装配
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- autowire 开启自动装配-->
<bean id="dog" class="cc.anti.Dog"></bean>
<bean id ="people" class="cc.anti.People" autowire="byName"></bean>
</beans>
package cc.anti;
public class Dog {
@Override
public String toString() {
return "dog";
}
}
package cc.anti;
import cc.anti.Cat;
import cc.anti.Dog;
public class People {
private Dog dog;
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "people+" + cat.toString() +"+" +dog.toString();
}
}
public void MyAutoTest() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-auto.xml");
People people = context.getBean("people", People.class);
System.out.printf(people.toString());
}
注解
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解-->
<context:annotation-config/>
<bean id="people" class="cc.anti.People" />
<bean id="dog" class="cc.anti.Dog" />
</beans>
package cc.anti;
public class Dog {
@Override
public String toString() {
return "dog";
}
}
package cc.anti;
import cc.anti.Cat;
import cc.anti.Dog;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
@Autowired //注入方式
private Dog dog;
@Override
public String toString() {
return "people+" + cat.toString() +"+" +dog.toString();
}
}
public void MyAutoAnnotateTest() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-annotate.xml");
People people = context.getBean("people", People.class);
System.out.printf(people.toString());
}