spring注解方式注入bean

时间:2023-03-09 17:55:45
spring注解方式注入bean

用注解的方式注入bean,spring的配置文件也要增加一些约束和导入注解所在的包

spring注解方式注入bean

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"> <context:component-scan base-package="com.xiaostudy.service"></context:component-scan>
<context:component-scan base-package="com.xiaostudy.dao"></context:component-scan>
</beans>

用注解注入的bean类PersonImple.java

 package com.xiaostudy.service;

 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;
/*
* bean注入
* 1、@Component("id")
* 2、WEB:
* @Controller("id") web
* @Service("id") service
* @Repository("id") dao
*/
@Component("person")
/*
* bean作用域
* @Scope("singleton") 单例(也是默认)
* @Scope("prototype") 每new一次都是新的对象
*/
@Scope("prototype")
public class PersonImple implements Person {
/*
* bean参数注入
* 1、普通参数(也就是说那些基本数据类型)
* 在参数上面或者在相应的set方法 @Value("值")
* 2、引用参数
* 2.1按照类型注入:@Autowired
* 2.2按照名称注入:@Autowired @Qualifier("id")
* 2.3按照名称注入:@Resource
*
*/
//@Resource
private Dao_demoImple dao_demo;
@Value("xiaostudy")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Dao_demoImple getDao_demo() {
return dao_demo;
} @Resource
public void setDao_demo(Dao_demoImple dao_demo) {
this.dao_demo = dao_demo;
}
/*
* bean初始化方法注解
* @PostConstruct
*/
@PostConstruct
public void init() {
System.out.println("init()>>>>>>");
} /*
* bean销毁注解
* @PreDestroy
*/
@PreDestroy
public void destroy() {
System.out.println("destroy()>>>>>");
} }

Person接口

 package com.xiaostudy.service;

 public interface Person {

 }

Dao_demo接口

 package com.xiaostudy.dao;

 public interface Dao_demo {

 }

Dao_demoImple.java

 package com.xiaostudy.service;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import com.xiaostudy.dao.Dao_demo;
@Component("dao_demo")
public class Dao_demoImple implements Dao_demo { private String name; public String getName() {
return name;
} @Value("demo")
public void setName(String name) {
this.name = name;
} public void add() {
System.out.println("add>>>>>>");
} }

测试类Test.java

 package com.xiaostudy.service;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xiaostudy.dao.Dao_demo;
import com.xiaostudy.service.Person; public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ac.getBean("person", Person.class);
System.out.println(person);
PersonImple pi = (PersonImple)person;
System.out.println(pi.getName());
Dao_demoImple dao_demo = pi.getDao_demo();
System.out.println(dao_demo);
dao_demo.add();
System.out.println(dao_demo.getName());
/*Person person1 = ac.getBean("person", Person.class);
Person person2 = ac.getBean("person", Person.class);
System.out.println(person1);
System.out.println(person2);
((AbstractApplicationContext) ac).close();*/
} }