用注解的方式注入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的更多相关文章

  1. spring注解方式注入

    1.通过Resource注入 1.在属性上注入 1.默认注入 即不指定spring容器里面的名字 匹配规则:先通过属性的名字查找 再通过属性类型与实现类类型匹配查找 当有两个实现类会报错 2.通过指定 ...

  2. spring 注解方式配置Bean

    Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...

  3. spring注解方式在一个普通的java类里面注入dao

    spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...

  4. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  5. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  6. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  7. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  8. Spring 注解方式 实现 IOC 和 DI

    注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframewor ...

  9. EJB通过注解方式注入并使用其他EJB或者服务、配置JBoss数据源

    通过注解方式注入并使用其他EJB或者服务 真实项目EJB对象很多,EJB之间也可以互相调用, 在项目HelloWorld下新建接口Other在cn.hqu.ejb3下: public interfac ...

随机推荐

  1. vue+node+mongoDB 火车票H5(二)---vux和less的配置

    vue基本环境配置好之后,就可以开始开发页面了 开发页面之前先了解一下项目结构,原始的目录结构为: config是配置文件,环境配置好了开发阶段可以不再去修改了,node_modules文件夹是打包的 ...

  2. 巨蟒python全栈开发-第7天 基本数据类型补充&深浅拷贝

    1.基本数据类型补充 2.深浅拷贝 DAY7-基本数据类型(基本数据类型补充&深浅拷贝) 本节主要内容: 1.补充基础数据类型 (1)join方法 (2)split方法 (3)列表不能在循环时 ...

  3. git 学习(一)初始化和提交

    git 学习(一) 创建git版本库 $ mkdir gitstudy $ cd gitstudy $ git init nitialized empty Git repository in /Use ...

  4. WikiMedia system architecture

    w 前端  服务端 后端

  5. 我的Android进阶之旅------>Android编译错误java.util.zip.ZipException: duplicate entry的解决方法

    今天在Android Studio中把另外一个项目引入当前项目,编译的时候出现了java.util.zip.ZipException: duplicate entry错误. 错误如下所示: FAILU ...

  6. OpenSSL和https原理

    https原理: 浏览器请求服务端的公钥证书,server将注冊的证书发送给client. client向办法机构验证证书的合法性,证书 包含公钥,server网址及一些信息. 验证完成,client ...

  7. Java中对Clone的理解

    面试中经常遇到Clone的相关知识,今天总算是把Clone理解的比较透彻了!Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Cl ...

  8. C++和JAVA实例化对象的区别

    JAVA: A a = new A(); 为A对象创建了一个实例,但在内存中开辟了两块空间:一块空间在堆区,存放new A()这个对象:另一块空间在堆栈,也就是栈,存放a,a的值为new A()这个对 ...

  9. Python-selenium 下拉框定位

    1.通过select 进行定位下拉框 首先selenium 很人性化的给提供了一个Select的模块,供处理下来菜单,首先我们需要导入Select,通过from selenium.webdriver. ...

  10. python删除所有的中文字符、非ASCII或非英文字符,检查字符串是否包含非ASCII

    Your ''.join() expression is filtering, removing anything non-ASCII; you could use a conditional exp ...