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

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();*/
} }
spring注解方式注入bean的更多相关文章
- spring注解方式注入
1.通过Resource注入 1.在属性上注入 1.默认注入 即不指定spring容器里面的名字 匹配规则:先通过属性的名字查找 再通过属性类型与实现类类型匹配查找 当有两个实现类会报错 2.通过指定 ...
- spring 注解方式配置Bean
Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...
- spring注解方式在一个普通的java类里面注入dao
spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- Spring 注解方式 实现 IOC 和 DI
注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframewor ...
- EJB通过注解方式注入并使用其他EJB或者服务、配置JBoss数据源
通过注解方式注入并使用其他EJB或者服务 真实项目EJB对象很多,EJB之间也可以互相调用, 在项目HelloWorld下新建接口Other在cn.hqu.ejb3下: public interfac ...
随机推荐
- 【BZOJ4382】[POI2015]Podział naszyjnika 堆+并查集+树状数组
[BZOJ4382][POI2015]Podział naszyjnika Description 长度为n的一串项链,每颗珠子是k种颜色之一. 第i颗与第i-1,i+1颗珠子相邻,第n颗与第1颗也相 ...
- ssh访问跳过RSA key"yes/no"验证
通常我们再批量配置多台机器的时候经常出现通过ssh批量登录机器提示 RSA key fingerprint is ::a6:b1:c9:d7:b8::c1:::8e:f5::2b:8b. Are yo ...
- Powershell Exchange Server UP Time
Server up time Get-ExchangeServer | where{$_.name -like'wendy*'} | %{ if(Test-Connection $_.name -Co ...
- 在Windows 7 64位下注册ActiveX失败的解决办法
copy Chart10W.dll %SystemRoot%\SysWOW64\ copy cellweb5.inf %SystemRoot%\SysWOW64\ copy hado.dll %S ...
- 深入理解CNI
1.为什么会有CNI? CNI是Container Network Interface的缩写,简单地说,就是一个标准的,通用的接口.已知我们现在有各种各样的容器平台:docker,kubernetes ...
- django博客项目5:博客首页视图(2)
真正的 Django 博客首页视图 在此之前我们已经编写了 Blog 的首页视图,并且配置了 URL 和模板,让 Django 能够正确地处理 HTTP 请求并返回合适的 HTTP 响应.不过我们仅仅 ...
- sql server中index的REBUILD和REORGANIZE的区别及工作方式
sql server中index的REBUILD和REORGANIZE 转自:https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451 ...
- 使用jqueryUI和corethink实现的类似百度的搜索提示
代码:http://download.csdn.net/detail/u012995856/9676845 效果: 目录: 这里是以corethink模块的形式,只需要安装上访问 index.php? ...
- C语言自带快速排序对比插入排序
#include <stdio.h> #include <stdlib.h> #include <time.h> void getRandomArr (int ar ...
- s5_day9作业
# 1 编写 tail -f a.txt |grep 'error' |grep '404'命令,周一默写 # import time # def tail(filepath,encoding='ut ...