Spring的注解装配方式

后续还会补充更多的注释

目录

简单的注入实例

自动注入 @AutoWired

自动装配时的歧异问题 @Qualifier (ps:我老是记不住这个单词)


简单的注入实例

  • Component

  • ComponentScan
  • @Value
  • AnnotationConfigApplicationContext
package com.qyc.SpringNoteComment;

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

@Component()
public class StudentPro {
	@Value("强月城")
	private String name;
	@Value("20")
	private String age;
	
	public StudentPro() {
		super();
		// TODO Auto-generated constructor stub
	}
	public StudentPro(String name, String age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "StudentPro [name=" + name + ", age=" + age + "]";
	}
	
}
package com.qyc.SpringNoteComment;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan()
public class PojoConfig {
	
}
package com.qyc.SpringNoteComment;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration("classpath:spring-config.xml")
public class TestDemo {
//	private StudentPro studentPro;
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PojoConfig.class);
		StudentPro studentPro = (StudentPro) applicationContext.getBean(StudentPro.class);
		System.out.println(studentPro);
	}
}

StudentPro [name=强月城, age=20]

 

自动注入 @AutoWired

  • @AutoWired可以在属性前也可以在set方法前
  • 根据类型找到对应的bean自动注入
  • <context:component-scan base-package="com.qyc.autoWired"></context:component-scan>   扫描
  • <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>  开启注释功能

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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.qyc.autoWired"></context:component-scan>
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
</beans>
package com.qyc.autoWired;

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

@Component
public class Student {
	@Value( value = "强月城")
	private String name;
	@Value( value = "2017011418")
	private String id;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", id=" + id + "]";
	}
	
}
package com.qyc.autoWired;

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

@Component
public class StudentMethodImpl implements StudentMethod{
	@Autowired
	public Student student = null;
	

	public void printStudent() {
		System.out.println(student.toString());
	}
}
package com.qyc.autoWired;

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

public class TestDemo {
	public static void main(String[] args) {
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PojoConfig.class);
		StudentMethodImpl studentMethodImpl = applicationContext.getBean(StudentMethodImpl.class);
		studentMethodImpl.printStudent();
	}
}

全注释要增加下面的类

package com.qyc.autoWired;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class PojoConfig {
	
}

 

一个接口有多个实现类

  • @component后默认类首字母小写为beanid
  • getBean("id")才可找到
  • 目前我没找到在面向切面编程时,直接类名.class的就能找到bean的方法

 

自动装配时的歧异问题 @Qualifier (ps:我老是记不住这个单词)

  • 一个接口有多个实现类时,在自动装配时IOC容器会懵
  • 它原来是按照接口类型装配的,当有多个实现时,它不知道要装配哪个,所以会抛异常
  • 只需要在@Autowired 下面加一个 @Qualifier 就可以了
  • @Qualifier(里面要填具体的Bean id)

接口

package com.qyc.Autowired;

public interface Student {
	public void show();
}

studnet实例1

package com.qyc.Autowired;

import org.springframework.stereotype.Component;

@Component
public class Studentzd implements Student{
	private String name;
	private String id;
	public Studentzd() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Studentzd(String name, String id) {
		super();
		this.name = name;
		this.id = id;
	}
	@Override
	public String toString() {
		return "Studentzd [name=" + name + ", id=" + id + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void show() {
		// TODO Auto-generated method stub
		System.out.println("我是zd");
	}
	
	
//	
}

student实例2

package com.qyc.Autowired;

import org.springframework.stereotype.Component;

@Component
public class StudnetzdPro implements Student{

	public void show() {
		// TODO Auto-generated method stub
		System.out.println("我是Pro");
	}
	
}

StudentMethod接口

package com.qyc.Autowired;

public interface StudentzdMethod {
	public void printStu();
}

StudentMethod实例1

package com.qyc.Autowired;

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

@Component
public class StudentzdMethodImpl implements StudentzdMethod{
@Autowired
@Qualifier("studentzd")
private Student student = null;
	public void printStu() {
		// TODO Auto-generated method stub
		student.show();
	}
	
}

实例2

package com.qyc.Autowired;

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

@Component
public class StudentzdMethodImpl2 implements StudentzdMethod{
@Autowired
@Qualifier("studnetzdPro")
private Student student = null;
	public void printStu() {
		// TODO Auto-generated method stub
		System.out.println("StudentzdMethodImpl2");
		student.show();
	}

}

不能没有的一个配置类

package com.qyc.Autowired;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class ConfigClass {

}

TestDemo

package com.qyc.Autowired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.qyc.MoreAopXml.StudnetMethod;

public class TestDemo {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigClass.class);
		StudentzdMethod studentzdMethod = (StudentzdMethod) applicationContext.getBean(StudentzdMethodImpl2.class);
		studentzdMethod.printStu();
	}
}

StudentzdMethodImpl2
我是Pro

 自动装配构造方法

package com.qyc.Autowired;

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

@Component
public class StudentzdMethodImpl2 implements StudentzdMethod{

private Student student = null;

	public StudentzdMethodImpl2(@Autowired @Qualifier("studnetzdPro")Student student) {
	super();
	this.student = student;
	System.out.println("调用了构造方法");
}

	public void printStu() {
		// TODO Auto-generated method stub
		System.out.println("StudentzdMethodImpl2");
		student.show();
	}

}

调用了构造方法
DEBUG 11-22 15:18:11,024 Eagerly caching bean 'studentzdMethodImpl2' to allow for resolving potential circular references  (AbstractAutowireCapableBeanFactory.java:539) 
DEBUG 11-22 15:18:11,024 Finished creating instance of bean 'studentzdMethodImpl2'  (AbstractAutowireCapableBeanFactory.java:485) 
DEBUG 11-22 15:18:11,024 Returning cached instance of singleton bean 'studnetzdPro'  (AbstractBeanFactory.java:251) 
DEBUG 11-22 15:18:11,024 Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'  (AbstractBeanFactory.java:251) 
DEBUG 11-22 15:18:11,055 Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@396e2f39]  (AbstractApplicationContext.java:784) 
DEBUG 11-22 15:18:11,055 Returning cached instance of singleton bean 'lifecycleProcessor'  (AbstractBeanFactory.java:251) 
DEBUG 11-22 15:18:11,055 Could not find key 'spring.liveBeansView.mbeanDomain' in any property source  (PropertySourcesPropertyResolver.java:91) 
DEBUG 11-22 15:18:11,055 Returning cached instance of singleton bean 'studentzdMethodImpl2'  (AbstractBeanFactory.java:251) 
StudentzdMethodImpl2
我是Pro

 

@Bean装配Bean

  • 方法的返回值为Bean,存放到IOC容器中
  • 默认为bean为方法名    (DEBUG 11-22 15:37:02,931 Finished creating instance of bean 'getCrazyLL'  (AbstractAutowireCapableBeanFactory.java:485) )
package com.qyc.Autowired;

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

@Component
public class StudentzdMethodImpl1 implements StudentzdMethod{

	public void printStu() {
		// TODO Auto-generated method stub
		System.out.println("StudentzdMethodImpl1");
	}
	@Bean
	public CrazyLL getCrazyLL() {
		CrazyLL crazyLL = new CrazyLL("刘琳", "她疯了");
		return crazyLL;
	}

}

TestDemo 

package com.qyc.Autowired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.qyc.MoreAopXml.StudnetMethod;

public class TestDemo {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigClass.class);
		StudentzdMethodImpl1 studentzdMethod = (StudentzdMethodImpl1) applicationContext.getBean("studentzdMethodImpl1");
		CrazyLL crazyLL1 = studentzdMethod.getCrazyLL();
		System.out.println(crazyLL1);
		
		CrazyLL crazyLL2 = (CrazyLL) applicationContext.getBean("getCrazyLL");
		System.out.println(crazyLL2);
	}
} 

CrazyLL [nameString=刘琳, noteString=她疯了]
DEBUG 11-22 15:37:02,962 Returning cached instance of singleton bean 'getCrazyLL'  (AbstractBeanFactory.java:251) 
CrazyLL [nameString=刘琳, noteString=她疯了]

 

 

Bean的作用域

  • @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 声明原型
  • @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) 声明单例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值