用注解的方式注入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. iOS tableview滑动到底部自动加载,向上拽加载

    - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { CGPoint offset = aScrollView.contentOffset ...

  2. 爬虫入门【9】Python链接Excel操作详解-openpyxl库

    Openpyx是一个用于读写Excel2010各种xlsx/xlsm/xltx/xltm文件的python库. 现在大多数用的都是office2010了,如果之前之前版本的可以使用xlrd读,xlwt ...

  3. Java基础语法 - 面向对象 - this 关键字

    在Java语言中规定使用this关键字来代表本类对象的引用,this关键字被隐式地用于引用对象的成员变量和方法. this关键字引用的就是本类的一个对象,在局部变量或方法参数覆盖了成员变量时,就要添加 ...

  4. 模块 - random/string/os/sys/shutil/zipfile/tarfile

    random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...

  5. 6.Insert Documents-官方文档摘录

    总结 1.插入单文档 db.inventory.insertOne( { item: "canvas", qty: , tags: , w: 35.5, uom: "cm ...

  6. Python获取指定路径下所有文件的绝对路径

    需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...

  7. python collection 和 heapq 模块使用说明

    一 :集合库collection python 拥有一些内置的数据类型,collections模块提供啦几个额外的数据类型: 1,namedtuple   生成可以使用名字来访问元素内容的tuple子 ...

  8. [动态规划]UVA10285 - Longest Run on a Snowboard

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  9. 004-React入门概述

    一.概述 参考地址:https://reactjs.org/docs/try-react.html 1.1.本地快速体验 <!DOCTYPE html> <html> < ...

  10. java 多线程 day04 线程通信

    package com.czbk.thread; /** * Created by chengtao on 17/12/3. * 需求: 子线程先运行10次,然后主线程运行 100次,依次运行50次 ...