手动装配Bean
代码:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
} class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
} class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
} class Person {
private String name;
private Address address;
private Car car; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + ", car=" + car
+ "]";
}
}
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 手动装配Bean -->
<bean id="address" class="com.company.Address"
p:city="WhuHan" p:street="Bayi"></bean> <bean id="car" class="com.company.Car"
p:brand="Audi" p:price="300000"></bean> <bean id="person" class="com.company.Person"
p:name="Roger" p:address-ref="address" p:car-ref="car"></bean> </beans>
结果:
Person [name=Roger, address=Address [city=WhuHan, street=Bayi], car=Car [brand=Audi, price=300000.0]]
对applicationContext.xml进行修改
<bean id="person" class="com.company.Person"
p:name="Roger" ></bean>
结果:
Person [name=Roger, address=null, car=null]
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 手动装配Bean -->
<bean id="address" class="com.company.Address"
p:city="WhuHan" p:street="Bayi"></bean> <bean id="car" class="com.company.Car"
p:brand="Audi" p:price="300000"></bean> <bean id="person" class="com.company.Person"
p:name="Roger" autowire="byName"></bean>
</beans>
Person [name=Roger, address=Address [city=WhuHan, street=Bayi], car=Car [brand=Audi, price=300000.0]]
使用自动装配auotwire="byName"
将上面的autowire改成byType一样可以运行,结果也一样。
自动检测Bean
<context:component-scan
base-package="com.company">
</context:component-scan>
https://my.oschina.net/u/1020238/blog/502599
https://my.oschina.net/u/1020238/blog/502930
http://blog.csdn.net/evankaka/article/details/45023835
手动装配Bean的更多相关文章
- 依赖注入Bean属性——手动装配Bean
一.构造方法注入 其中,可以根据不同的参数列表调用不同的重载的构造方法: 其中,基本数据类型没有包,引用类型都有包路径,基本类型对应封装类: 二.通过property标签调用类的set方法注入 三.通 ...
- java之Spring(IOC)装配Bean(手动装配、自动装配、注解装配)
在上一篇控制反转中我们看到了依靠一个Bean文件来实现对代码的控制,可谓十分便捷,再也不用去实例化对象了,2333~~~ 1.手动装配 <bean id="todo" cla ...
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- java之Spring(IOC)注解装配Bean详解
在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看Annotation的魅力所在吧. 先来看看之前的bean ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- Spring------自动化装配Bean(三)
上一篇是基于java手动装配bean的实现,这一篇将通过xml手动装配bean来实现. xml配置相对于java配置有点: xml配置更加快捷 但不宜扩展 一.打开application.xml 1. ...
- Spring------自动化装配Bean(二)
上一篇是基于 @ComponentScan自动装配Bean的实现,这一篇将通过java手动装配bean来实现. 手动装配相对于自动装配的优点: 可以自行定义Bean的各个属性. 添加额外的方法调度. ...
- Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring 框架 详解 (三)-----IOC装配Bean
IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...
随机推荐
- poj3666 Making the grade【线性dp】
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions:10187 Accepted: 4724 ...
- 20165330 学习基础和C语言基础调查
学习基础和C语言基础调查 读做中学有感 读了老师的推送,通过邹欣老师的博客中对老师和学生的关系比作教练和学员的阐述,这里重点为我们阐述了「做中学(Learning By Doing)」的重要性. 套路 ...
- ipconfig /flushdns
C:\Users\sas>ipconfig /flushdns Windows IP 配置 已成功刷新 DNS 解析缓存. C:\Users\sas>ipconfig --help 错误: ...
- 更新openssl
在安装nodejs或者nginx什么的时候,有时候会报如下错误 npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.s ...
- [LeetCode] 1.Two Sum - Swift
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- Pandas -- SettingwithCopyWarning 原理和解决方案(转)
本文对产生 SettingwithCopyWarning 的原因以及解决方案,做了详细解说. 详见: https://www.jianshu.com/p/72274ccb647a
- java-mybaits-00301-SqlMapConfig
1.配置内容 mybatis的全局配置文件SqlMapConfig.xml,SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) t ...
- 『HTML5实现人工智能』小游戏《井字棋》发布,据说IQ上200才能赢【算法&代码讲解+资源打包下载】
一,什么是TicTacToe(井字棋) 本游戏为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
- django生产环境部署
测试环境:linux centos7下 1.安装uwsgi python3下安装: pip3 install uwsgi python2下安装: pip install uwsgi 如果是系统自带的p ...
- Springboot+shiro配置笔记+错误小结
软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...