注解
1.使用注解配置spring
1)开启使用注解代理配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 指定扫描cn.mf.bean报下的所有类中的注解. 注意:扫描包时.会扫描指定报下的所有子孙包-->
<context:component-scan base-package="cn.mf.bean"></context:component-scan>
</beans>
2.在类中使用注解完成配置
1)将对象注册到容器
//@Component("user")相当于//<bean name="user" class="cn.itcast.bean.User" /> //@Service("user") // service层 //@Controller("user") // web层 @Repository("user")// dao层
2)修改对象的作用范围
//指定对象的作用范围 @Scope(scopeName="singleton")
3)值类型注入
通过反射的Field赋值,破坏了封装性
@Value("18") private Integer age;
通过set方法赋值,推荐使用.
@Value("tom") public void setName(String name) { this.name = name; }
4)引用类型注入
@Autowired //自动装配 private Car car;
@Autowired //自动装配 @Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象 private Car car;
@Resource(name="car")//手动注入,指定注入哪个名称的对象 private Car car;
5)初始化|销毁方法
@PostConstruct //在对象被创建后调用.init-method public void init(){ System.out.println("我是初始化方法!"); } @PreDestroy //在销毁之前调用.destory-method public void destory(){ System.out.println("我是销毁方法!"); }
实战总结
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!-- 指定扫描cn.mf.bean报下的所有类中的注解. 注意:扫描包时.会扫描指定报下的所有子孙包--> <context:component-scan base-package="cn.mf.bean"></context:component-scan> <bean name="car2" class="cn.mf.bean.Car" > <property name="name" value="布加迪威龙" ></property> <property name="color" value="black" ></property> </bean> </beans>
User.java
package cn.mf.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; //<bean name="user" class="cn.itcast.bean.User" /> //@Component("user") //@Service("user") // service层 //@Controller("user") // web层 @Repository("user")// dao层 //指定对象的作用范围 @Scope(scopeName="singleton") public class User { private String name; @Value("18") private Integer age; //@Autowired //自动装配 //问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象. //@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象 @Resource(name="car")//手动注入,指定注入哪个名称的对象 private Car car; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getName() { return name; } @Value("tom") public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @PostConstruct //在对象被创建后调用.init-method public void init(){ System.out.println("我是初始化方法!"); } @PreDestroy //在销毁之前调用.destory-method public void destory(){ System.out.println("我是销毁方法!"); } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } }
Car.java
package cn.mf.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("car") public class Car { @Value("玛莎拉蒂") private String name; @Value("呕吐绿") private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
Junit
@Test public void fun1(){ //1 创建容器对象 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2 向容器"要"user对象 User u1 = (User) ac.getBean("user"); User u2 = (User) ac.getBean("user"); System.out.println(u1==u2); //3 打印user对象 System.out.println(u1); ac.close(); }