Spring使用注解代替XML配置
2017-07-11 14:07
441 查看
1.导入命名空间
XML Catalog中除了要导入
2.在applicationContext.xml文件中,添加
其中,
3.为要创建Bean对象的JAVABean对象添加注解:
4.设置Bean对象的作用范围
5.属性注入
在要注入属性的Set方法上添加注解:
6.生命周期
Demo:
运行结果:
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...
相关文章推荐
- Spring系列之五:使用注解代替xml配置
- spring注解(三)使用注解代替xml配置
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- Spring中使用注解代替xml配置文件
- spring框架,可以没有spring.xml(即配置<beans>的文件吗?即不使用xml方式配置,而是只使用注解的方式配置
- Spring通过XML配置c3p0连接池和dao层注解注入使用 jdbcTemplate
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 当Spring同时使用了XML配置和注解
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- Springboot 中同时使用mybatis注解和springbean-xml配置方式
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- Spring使用AspectJ注解和XML配置实现AOP
- spring aop的使用(注解方式以及基于xml配置方式)
- spring aop的使用(注解方式以及基于xml配置方式)
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- 使用注解代替Spring配置文件
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- (笔记)Spring实战_最小化Spring XML配置(2)_使用注解装配
- 简化Spring的XML配置(二)——使用注解装配bean
- 使用注解反射实现BaseDao(代替XML配置)