spring 配置bean的方法及依赖注入发方式
2017-08-16 12:23
603 查看
Bean 的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean
这里依据全类名配置bean
<bean id="helloWord" class="com.spring.HelloWord">
<property name="userName" value="springsss"></property>
</bean>
依赖注入发方式:
属性注入:
applicationContext.xml配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWord" class="com.spring.HelloWord">
<property name="userName" value="springsss"></property>
</bean>
</beans>
package com.spring;
public class HelloWord {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public void hello() {
System.out.println("hello:" + userName);
}
public HelloWord(){
System.out.println("construct!!!!!!!!!!");
}
}
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
/**
* 创建对象及为对象赋值交给spring完毕
*/
// HelloWord helloWord = new HelloWord();
// helloWord.setUserName("hello");
// helloWord.hello();
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2从容器中获得Bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//3.调用方法
helloWord.hello();
}
}
输出结果为:
construct!!!!!!!!!!
hello:springsss
构造器注入:
applicationContext.xml文件为
<?xml version="1.0" encoding="UTF-8"?
>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="car" class="com.spring.Car">
<constructor-arg value="green"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
</bean>
</beans>
public class Car {
private String name;
private String color;
private int num;
public Car(String color, int num) {
super();
this.color = color;
this.num = num;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + ", num=" + num + "]";
}
}
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car.toString());
}
}
输出结果为:
Car [name=null, color=green, num=22]
这里依据全类名配置bean
<bean id="helloWord" class="com.spring.HelloWord">
<property name="userName" value="springsss"></property>
</bean>
依赖注入发方式:
属性注入:
applicationContext.xml配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWord" class="com.spring.HelloWord">
<property name="userName" value="springsss"></property>
</bean>
</beans>
package com.spring;
public class HelloWord {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public void hello() {
System.out.println("hello:" + userName);
}
public HelloWord(){
System.out.println("construct!!!!!!!!!!");
}
}
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
/**
* 创建对象及为对象赋值交给spring完毕
*/
// HelloWord helloWord = new HelloWord();
// helloWord.setUserName("hello");
// helloWord.hello();
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2从容器中获得Bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//3.调用方法
helloWord.hello();
}
}
输出结果为:
construct!!!!!!!!!!
hello:springsss
构造器注入:
applicationContext.xml文件为
<?xml version="1.0" encoding="UTF-8"?
>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="car" class="com.spring.Car">
<constructor-arg value="green"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
</bean>
</beans>
public class Car {
private String name;
private String color;
private int num;
public Car(String color, int num) {
super();
this.color = color;
this.num = num;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + ", num=" + num + "]";
}
}
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car.toString());
}
}
输出结果为:
Car [name=null, color=green, num=22]
相关文章推荐
- spring 配置bean的方法及依赖注入发方式
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- spring学习总结(三):IOC & DI 配置 Bean 之配置形式及依赖注入方式
- Spring4深入理解IOC&DI04----Bean配置方式(全类名,工厂方法,FactoryBean),配置形式(基于XML和注解),泛型依赖注入
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- Spring-装配Bean的3种方式-工厂静态方法方式配置Bean
- spring声明式事务配置方法(四):BeanNameAutoProxyCreator注入Bean
- Spring中利用AOP对领域对象(非依赖注入的对象,如new操作符、反射的方式生存的对象)下面是spring配置文件的一部分 <context:load-time-weaver /> <be
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Spring的配置、生命周期、依赖注入bean的操作
- 【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- 7.7.3: Bean实例的创建方式及依赖配置---调用实例工厂方法创建Bean
- spring通过注解依赖注入和获取xml配置混合的方式
- 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入
- Spring依赖注入的过程,ref注入与内部bean方式注入
- 7.10.3: 深入理解依赖关系配置---注入其他bean的方法返回值
- 实习总结(十)---Spring的下载/安装/优点/依赖注入方式/自动配置
- spring的配置文件中bean的注入方式
- 11级_Java_曹建波 04.24 Spring的配置、生命周期、依赖注入bean的操作