Spring-依赖注入
2014-09-26 11:39
183 查看
1.创建项目
spring092601
2.引入spring jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
3.添加配置文件
在conf下添加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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
4.创建业务bean
在src目录下创建
包名:cn.jbit.spring092601.domain
/**
* spring入门
* @author Administrator
*
*/
public class HelloSpring implements Serializable {
private String name;
public HelloSpring() {
System.out.println("调用无参构造方法");
}
public HelloSpring(String name) {
super();
this.name = name;
}
public void setName(String name) {
System.out.println("set");
this.name = name;
}
public String getName() {
System.out.println("get");
return name;
}
}
5.在配置文件中编写bean的配置,并注入属性值,也就是我们的DI(Dependency Injection)依赖注入
配置如下:
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- spring入门
控制反转IOC
默认构造方法装配Bean
-->
<bean id="helloSpring" class="cn.jbit.spring092601.domain.HelloSpring">
<!--
给属性注入一个值:hello
-->
<property name="name">
<value>hello</value>
</property>
</bean>
</beans>
6.测试bean的属性注入
在test目录下测试
包名:cn.jbit.spring092601.domain
public class HelloSpringTest {
/**
* 依赖注入方式实现
*/
@Test
public void testHellpSpring3(){
/*
* 对象由spring创建
*/
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
System.out.println(helloSpring.getName());
}
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1558443
spring092601
2.引入spring jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
3.添加配置文件
在conf下添加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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
4.创建业务bean
在src目录下创建
包名:cn.jbit.spring092601.domain
/**
* spring入门
* @author Administrator
*
*/
public class HelloSpring implements Serializable {
private String name;
public HelloSpring() {
System.out.println("调用无参构造方法");
}
public HelloSpring(String name) {
super();
this.name = name;
}
public void setName(String name) {
System.out.println("set");
this.name = name;
}
public String getName() {
System.out.println("get");
return name;
}
}
5.在配置文件中编写bean的配置,并注入属性值,也就是我们的DI(Dependency Injection)依赖注入
配置如下:
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- spring入门
控制反转IOC
默认构造方法装配Bean
-->
<bean id="helloSpring" class="cn.jbit.spring092601.domain.HelloSpring">
<!--
给属性注入一个值:hello
-->
<property name="name">
<value>hello</value>
</property>
</bean>
</beans>
6.测试bean的属性注入
在test目录下测试
包名:cn.jbit.spring092601.domain
public class HelloSpringTest {
/**
* 依赖注入方式实现
*/
@Test
public void testHellpSpring3(){
/*
* 对象由spring创建
*/
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
System.out.println(helloSpring.getName());
}
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1558443
相关文章推荐
- 深入分析JavaWeb 51 -- Spring依赖注入
- Spring - lookup-method方式实现依赖注入
- spring-依赖注入
- Spring从菜鸟到高手(三)依赖注入
- Spring 3.0 基于Annotation的依赖注入实现详解
- Servlet不能通过Spring依赖注入
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
- 1.2.2 依赖注入容器/框架 Spring
- Spring 学习笔记 - IOC/依赖注入
- Spring:依赖注入(注解方式)
- Spring系列之四——Spring IOC依赖注入
- Spring学习--泛型依赖注入
- spring入门(依赖注入方式,生命周期,读取属性文件值,数据库接连)
- 什么是spring.net?什么又是依赖注入呢?
- 在ABAP里模拟实现Java Spring的依赖注入
- spring四种依赖注入方式
- spring核心之依赖注入(一)设置注入
- 使用Spring.Net对Web页面进行依赖注入
- 谈谈对Spring 控制反转(IOC)及依赖注入(DI)的理解
- Spring注入依赖