使用注解代替Spring配置文件
2018-01-28 13:52
417 查看
使用注解代替Spring配置文件具体步骤
创建WEB工程-导包-为主配置文件引入新的命名空间(约束)-开启使用注解代理配置文件-在类中使用注解完成配置
1 导包
springsource.org.aopalliance-1.0.0.jar
springsource.org.apache.commons.logging-1.1.1.jar
springsource.org.apache.log4j-1.2.15.jar
springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE.jar
2 为主配置文件引入新的命名空间(约束)-context约束
3 开启使用注解代理配置文件
4.1 将对象注册到容器
值类型注入
引用类型注入
将car对象注入到容器
4.5 初始化|销毁方法
@PostConstruct//在对象被创建后调用
public void init(){
System.out.println("我是初始化方法");
}
@PreDestroy//在对象销毁之前调用 public void destroy(){
System.out.println("销毁方法");
}
附录
测试代码package com.hciot.a_annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hciot.bean.User;
public class Demo {
@Test
public void fun1(){
//1.创建容器对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器要user对象
User u = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user");
//3.打印user对象
System.out.println(u2);
ac.close();
//System.out.println(u2==u3);
}
}
User对象
package com.hciot.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//相当于是配置文件中的<bean name="user" class="com.hciot.bean.User"></bean> @Component("user") //@Service("user") //service层 //@Controller("user") //web层 //@Repository("user") //Dao层
//指定对象的作用范围
//@Scope(scopeName="prototype")
@Scope(scopeName="singleton")
public class User {
private String name;
private Integer age;
//@Autowired
//@Qualifier("car2")//使用Qualifier注释告诉spring容器自动装配哪个对象
@Resource(name="car2")
private Car car;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
//给对象注入属性值 @Value("tom") public void setName(String name) { this.name = name; } @Value("28") public void setAge(Integer age) { this.age = age; }
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@PostConstruct//在对象被创建后调用
public void init(){
System.out.println("我是初始化方法");
}
@PreDestroy//在对象销毁之前调用
public void destroy(){
System.out.println("销毁方法");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
配置文件
<?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 ">
<!-- 指定扫描com.hciot.bean包下的所有类中的注解.注意:扫描包时.会扫描指定报下的所有子孙包 --> <context:component-scan base-package="com.hciot.bean"></context:component-scan>
<bean name="car2" class="com.hciot.bean.Car">
<property name="name" value="奔驰"></property>
<property name="color" value="黄色"></property>
</bean>
</beans>
创建WEB工程-导包-为主配置文件引入新的命名空间(约束)-开启使用注解代理配置文件-在类中使用注解完成配置
1 导包
springsource.org.aopalliance-1.0.0.jar
springsource.org.apache.commons.logging-1.1.1.jar
springsource.org.apache.log4j-1.2.15.jar
springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE.jar
2 为主配置文件引入新的命名空间(约束)-context约束
3 开启使用注解代理配置文件
<!-- 指定扫描com.hciot.bean包下的所有类中的注解.注意:扫描包时.会扫描指定报下的所有子孙包 --> <context:component-scan base-package="com.hciot.bean"></context:component-scan>4 在类中使用注解完成配置
4.1 将对象注册到容器
//相当于是配置文件中的<bean name="user" class="com.hciot.bean.User"></bean> @Component("user") //@Service("user") //service层 //@Controller("user") //web层 //@Repository("user") //Dao层
测试结果 User [name=null, age=null, car=null]4.2 修改对象的作用范围
//指定对象的作用范围 //@Scope(scopeName="prototype") @Scope(scopeName="singleton")4.3
值类型注入
//给对象注入属性值 @Value("tom") public void setName(String name) { this.name = name; } @Value("28") public void setAge(Integer age) { this.age = age; }
测试结果 User [name=tom, age=28, car=null]4.4
引用类型注入
将car对象注入到容器
package com.hciot.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("car") public class Car { private String name; private String color; @Value("宝马") public void setName(String name) { this.name = name; } @Value("黄色") public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
@Autowired // 自动装配,但是当存在多个类型一致的对象时,将无法选择具体注入哪一个对象 private Car car;
测试结果 User [name=tom, age=28, car=Car [name=宝马, color=黄色]]
@Autowired @Qualifier("car2")//使用Qualifier注释告诉spring容器自动装配哪个对象 private Car car;
测试结果 User [name=tom, age=28, car=Car [name=奔驰, color=黄色]]
@Resource(name="car2") //手动注入,指定注入哪个对象 private Car car;
4.5 初始化|销毁方法
@PostConstruct//在对象被创建后调用
public void init(){
System.out.println("我是初始化方法");
}
@PreDestroy//在对象销毁之前调用 public void destroy(){
System.out.println("销毁方法");
}
我是初始化方法 User [name=tom, age=28, car=Car [name=奔驰, color=黄色]] 销毁方法
附录
测试代码package com.hciot.a_annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hciot.bean.User;
public class Demo {
@Test
public void fun1(){
//1.创建容器对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器要user对象
User u = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user");
//3.打印user对象
System.out.println(u2);
ac.close();
//System.out.println(u2==u3);
}
}
User对象
package com.hciot.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//相当于是配置文件中的<bean name="user" class="com.hciot.bean.User"></bean> @Component("user") //@Service("user") //service层 //@Controller("user") //web层 //@Repository("user") //Dao层
//指定对象的作用范围
//@Scope(scopeName="prototype")
@Scope(scopeName="singleton")
public class User {
private String name;
private Integer age;
//@Autowired
//@Qualifier("car2")//使用Qualifier注释告诉spring容器自动装配哪个对象
@Resource(name="car2")
private Car car;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
//给对象注入属性值 @Value("tom") public void setName(String name) { this.name = name; } @Value("28") public void setAge(Integer age) { this.age = age; }
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@PostConstruct//在对象被创建后调用
public void init(){
System.out.println("我是初始化方法");
}
@PreDestroy//在对象销毁之前调用
public void destroy(){
System.out.println("销毁方法");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
配置文件
<?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 ">
<!-- 指定扫描com.hciot.bean包下的所有类中的注解.注意:扫描包时.会扫描指定报下的所有子孙包 --> <context:component-scan base-package="com.hciot.bean"></context:component-scan>
<bean name="car2" class="com.hciot.bean.Car">
<property name="name" value="奔驰"></property>
<property name="color" value="黄色"></property>
</bean>
</beans>
相关文章推荐
- Spring中使用注解代替xml配置文件
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- spring框架,可以没有spring.xml(即配置<beans>的文件吗?即不使用xml方式配置,而是只使用注解的方式配置
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring注解使用和与配置文件的关系
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- 关于使用注解的Spring配置文件的配置<context:annotation-config/>&&<context:component-scan base-package=”XX.XX”/>
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- 使用注解,实现ssh项目中spring配置文件的零配置,约定优于配置
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- spring使用注解时配置文件的配置
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring整合JUnit4测试时,使用注解引入多个配置文件
- spring注解和配置文件混合使用
- spring使用注解时配置文件的写法
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- (六)Spring详解——使用注解替代配置文件