Spring系列【07】零配置实现Bean的注入
2014-12-14 22:09
411 查看
![](http://images.cnitblog.com/blog/517520/201412/142202591349003.png)
与前几例不同,需要导入aop包。
Book.java 注意Book类@Component
package cn.com.xf; import org.springframework.stereotype.Component; @Component public class Book { private String name="JAVA从入门到精通"; private double price=45.67; public String getName() { return name; } @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Person.java Person类@Component
package cn.com.xf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Person { private String address="河南省南阳市"; @Autowired private Book book; public String getAddress() { return address; } @Override public String toString() { return "Person [address=" + address + ", book=" + book + "]"; } public void setAddress(String address) { this.address = address; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } }
Spring配置文件:注意<context:annotation-config/>
<?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-4.1.xsd"> <context:annotation-config/> <context:component-scan base-package="cn.com.xf"> </context:component-scan> </beans>
相关文章推荐
- Spring系列【04】应用@Autowired注解实现Bean的注入
- 【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- Spring零配置通过注解实现Bean依赖注入总结<转>
- 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入
- Spring系列【03】应用构造器注入法实现Bean的注入
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- 【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- Spring系列【06】@Resource注解实现Bean的注入
- 【第十二章】零配置 之 12.1 概述 12.2 注解实现Bean依赖注入 ——跟我学spring3
- 零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- 11级_Java_曹建波 04.24 Spring的配置、生命周期、依赖注入bean的操作
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
- Spring配置Bean中注入Enum实例
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置
- spring3——注解实现Bean依赖注入
- Spring学习笔记 使用annotation配置实现Bean的auto-wiring (自动绑定)
- 扩展Spring——使用 Annotation将配置资源注入到Bean中