spring--注解
1.导入命名空间
XML Catalog中除了要导入spring-beans以外,还要导入spring-context。
2.在applicationContext.xml文件中,添加
<context:component-scan base-package="Object"></context:component-scan>
其中,base-package中表示要扫描的包名。我这里表示扫描Object包及其子包下所有的注解。
3.为要创建Bean对象的JAVABean对象添加注解:
@Component("user") //user为Bean的name
//以下三个注解作用和Component完全相同,只是为了在MVC中方便查看
@Service("user")//service层
@Controller("user")//web层
@Repository("user")//dao层
4.设置Bean对象的作用范围
@Scope(scopeName="singleton") //单例
@Scope(scopeName="prototype") //多例
5.属性注入
在要注入属性的Set方法上添加注解:
//值类型
@Value(value="wxs") //wxs为属性值
//对象类型
@Resource(name="car") //car为Car类的Bean对象
6.生命周期
@PostConstruct//在对象被创建后调用
@PreDestroy//在对象被销毁前掉调用
Demo:
<!--src/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 "> <context:component-scan base-package="Object"></context:component-scan> </beans>
//Object.User.java: package Object; 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.Component; @Component("user") @Scope(scopeName="singleton") public class User { private String name; private Integer age; private Car car; public Car getCar() { return car; } @Resource(name="car") public void setCar(Car car) { this.car = car; } public String getName() { return name; } @Value("wxs") public void setName(String name) { this.name = name; } public Integer getAge() { return age; } @Value("18") public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } @PostConstruct public void init(){ System.out.println("init..."); } @PreDestroy public void destory() { System.out.println("destory..."); } } //Object.Car.java: package Object; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("car") public class Car { private String name; private String color; public String getName() { return name; } @Value("兰博基尼") public void setName(String name) { this.name = name; } public String getColor() { return color; } @Value("绿色") public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
//Demo.UserTest.java: package Demo; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import Object.User; public class UserTest { @Test public void f1(){ //这里只是为了能够调用ac.close()方法,才使用ClassPathXmlApplicationContext。正常应使用ApplicationContext ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User u = (User)ac.getBean("user"); System.out.println(u); ac.close(); } }
运行结果:
init...
User [name=wxs, age=18, car=Car [name=兰博基尼, color=绿色]]
destory...
---------------------