spring 第8天 Spring 注解

spring注解

@Component:标注一个普通的spring bean类
@Controller:标注一个控制器组件
@Service:标注一个业务逻辑组件
@Reponsitory:标注一个DAO组件

@Scope:配置bean的作用域不配置 默认为 singleton
@DependsOn({"aa"}):可以修饰bean和方法,表示在操作前 ,先初强制初始化 aa bean
@Lazy(false) :修饰bean类,用于指定该bean是否执行预初始化行为 默认为false :表示会执行初始化
@Autowired:可以修饰Field setter方法,普通方法,自动注入,默认采用byType注入
@Qualifier:精确的自动装配:可以修饰Field,setter方法,普通法方法,必须和@Autowired 一起使用,否则不会将找到的bean初始化后,赋值给属性.



package annotation.model.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import annotation.model.interfaces.Axe;
import annotation.model.interfaces.Dog;
import annotation.model.interfaces.Person;

@Component
public class Japanese implements Person {

	//定义一个方法
	@Autowired
	@Qualifier("steelAxe")
	public Axe axe;
	
	@Autowired
	public Dog dog;
	
	
	@Autowired  // 会找到所有的Axe 类型然后天津到 数组中
	private Axe[] axes;
	
	private Set<Axe> saxe;
	
	public Set<Axe> getSaxe() { 
		return saxe;
	}

	@Autowired  //如果 不指定  集合的类型 spring 将不知所措
	public void setSaxe(Set<Axe> saxe) {
		this.saxe = saxe;
	}

	public Axe getAxe() {
		return axe;
	}

	/**
	 * 如果发现 这一类型的 有多个Bean 会出现异常
	 * @param axe
	 */
	@Autowired   //找到 类型为Axe 的后,会自动注入, 默认采用 byType自动装配 
	public void setAxe(@Qualifier("stoneAxe") Axe axe) {
		System.out.println("bb");
		this.axe = axe;
	}

	@Autowired  //多个参数的普通方法
	public void prepare(@Qualifier("steelAxe") Axe axe,Dog dog){
		System.out.println("ss");
		this.axe=axe;
		this.dog=dog;
	}
	
	@Override
	public void useAxe() {
		System.out.println("正在使用"+getAxe()+"劈柴");
		System.out.println(getAxe().chop());
	}
	
	
	public void life(){
		dog.sound();
	}

	public Dog getDog() {
		return dog;
	}

	public Axe[] getAxes() {
		return axes;
	}

	public void setDog(Dog dog) {
		this.dog = dog;
	}

	public void setAxes(Axe[] axes) {
		this.axes = axes;
	}
	
	
}

  <context:component-scan base-package="annotation">
   		<!--  
   		type: 有4种类型  annotation assignable regex aspectj
   		-->
   		<!-- 包含下面结尾的类 -->
   		<context:include-filter type="regex" expression=".*Chinexe"/>
   		<context:include-filter type="regex" expression=".*Axe"/>
   		<context:include-filter type="assignable" expression="annotation.resources.TestBean"/>
   		
   		<!-- 不包含下面包下的所有类 -->
   		<context:exclude-filter type="regex" expression="annotation.model.interfaces.*"/>
   </context:component-scan>


ApplicationContext act=null;
	@Before
	public void init(){
		act=new ClassPathXmlApplicationContext("classpath:applicationContent_anno.xml");
	}

//测试 自动 注入 和精确注入  Autowired  Qualifier 
	@Test
	public void test1(){  // setMethod  > generalMethod(普通方法) > Field 
//输出spring中所有bean 的实例名称 
System.out.println(Arrays.toString(act.getBeanDefinitionNames()));
		Japanese j=act.getBean("japanese",Japanese.class);
		j.useAxe();
		j.life();
		System.out.println(Arrays.toString(j.getAxes()));
		
	}


定制Bean的生命周期行为

@PostConstruct :修饰 bean找那个init-method,和配置文件中init-menthod作用一样
@PreDestory:修改bean中的close方法 和配置文件中的属性 destory-method作用一样




	@PostConstruct  // 和 init-method 方法一样
	public void init(){
		System.out.println("PostConstruct: init-method:正在初始化....");
	}
	
	@PreDestroy  //和 destory-method 方法一样
	public void destroy() throws Exception {
		System.out.println("@PreDestroy:destory-method:注解,即将被销毁...");
	}
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值