spring中注解的使用

spring中使用注解对bean进行管理时,需要在applicationContext.xml文件中配置扫描。例如:

 <!-- 配置 注解Bean 所在包,让spring容器能够扫描到 -->
	 <context:component-scan base-package="com.my.annotation"></context:component-scan>
	 <!-- 使用注解注入,生命周期 --> 
	 <context:annotation-config />

使用注解声明Bean
XML 使用 <bean>标签定义bean对象
注解 使用 @Component 注解定义Bean 对象

第一步: 导入jar包
注解开发jar和xml开发jar 是相同的
第二步: 使用注解声明Bean

@Component("person") 
// 等价于 <bean id="person" class="..." 
public class Person {
}

Spring 2.5 版本提供 @Component 注解
在Spring2.5 还提供了三个和@Component 功能等价衍生注解
@Repository 用于对DAO实现类进行标注
@Service 用于对Service实现类进行标注
@Controller 用于对Controller实现类进行标注

提供这个三个注解目的,在开发中对Bean进行职责划分,期望在后续版本对三个注解进行增强

如果配置bean 没有起名字,默认名字就是类名 , 建议为bean 定义id 、name

Bean 属性依赖注入
定义 UserService 和 UserDAO
UserService 需要 依赖UserDAO
需要向UserService 注入一些属性

属性分为: 简单类型和复杂类型
Xml 简单 <property value />复杂 <property ref />
注解注入
简单类型 @Value注解 (Spring3.0 才有的 )

@Value("百度")
private String company; 

复杂类型 @Value注解 结合 SpEL 表达式

@Value("#{userDAO}")
private UserDAO userDAO ;

使用自动装配注解 @Autowired , 默认按照类型寻找对象.
Autowired注入时可以针对成员变量或者setter方法
但是,所有基于注解的注入,如果定义到成员变量上,底层都是通过反射注入 ,不需要setter方法

@Autowired + @Qualifier 注解实现按照名称注入

@Autowired // 自动装配(默认按类型注入,基于反射)
@Qualifier("userDAO") // 指定注入对象名称
private UserDAO userDAO ;

使用注解定义初始化和销毁的方法
<bean init-method=”xxx” destroy-method=”xxx”>通过 init-method和destroy-method 指定初始化和销毁方法
@PostContruct 实现初始化方法指定
@PreDestroy 指定销毁方法

通过@Scope注解 指定Bean作用域
如果不指定, 默认singleton

代码示例:
applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	 <!-- 配置 注解Bean 所在包,让spring容器能够扫描到 -->
	 <context:component-scan base-package="com.my.annotation"></context:component-scan>
	 <!-- 使用注解注入,生命周期 --> 
	 <context:annotation-config />
</beans>

Person.java

package com.my.annotation;

import org.springframework.stereotype.Component;

@Component("person") 
//@Service
//等价于 <bean id="person" class="..." 
public class Person {

}

UserDAO.java

package com.my.annotation;

import org.springframework.stereotype.Repository;

@Repository("userDAO")
public class UserDAO {

}

UserService.java

package com.my.annotation;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
	
	// 依赖属性 
	@Value("百度")
	private String company; 
	
	//@Value("#{userDAO}")
	
	//@Autowired // 自动装配(默认按类型注入,基于反射)
	//@Qualifier("userDAO") // 指定注入对象名称
	
	// 使用@Resource 取代 @Autowired
	@Resource(name="userDAO")
	private UserDAO userDAO ;
	
	@Override
	public String toString() {
		return "UserService [company=" + company + ", userDAO=" + userDAO + "]";
	}
	

}

LifeBean.java

package com.my.annotation;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Service;

@Service("lifeBean")
public class LifeBean {

	public LifeBean() {
		System.out.println("lifeBean 构造");
	}
	
	@PostConstruct // 初始化
	public void setup(){
		System.out.println("lifeBean 初始化...");
	}
	
	@PreDestroy // 销毁
	public void teardown(){
		System.out.println("lifeBean 销毁....");
	}
	
}

PrototypeBean.java

package com.my.annotation;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("prototypeBean")
@Scope("prototype")
public class PrototypeBean {
	
	public PrototypeBean() {
		System.out.println("PrototypeBean 被构造了...");
	}

}

Test.java

package com.my.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	
	// 测试 注解声明Bean
	@org.junit.Test
	public void demo1(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("person");
		System.out.println(person);
		Person person2 = applicationContext.getBean(Person.class);
		System.out.println(person2);
	}
	
	@org.junit.Test
	public void demo2(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService");
		System.out.println(userService);
	}
	
	@org.junit.Test
	public void demo3(){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		LifeBean lifeBean = (LifeBean) applicationContext.getBean("lifeBean");
		System.out.println(lifeBean);
		
		// 手动关闭容器
		applicationContext.close();
	}
	
	@org.junit.Test
	public void demo4(){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		PrototypeBean prototypeBean1 = (PrototypeBean) applicationContext.getBean("prototypeBean");
		PrototypeBean prototypeBean2 = (PrototypeBean) applicationContext.getBean("prototypeBean");
		
		System.out.println(prototypeBean1);
		System.out.println(prototypeBean2);
	}
	

}

Coding Diary

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值