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
package cn.jbit.spring092601.domain;
import java.io.Serializable;
/**
* 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
1.装配方式:默认构造方法方式来装配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 testHellpSpring5(){
/*
* 构造函数方式
* 对象由spring创建,调用无参构造方法来实例化Bean
*/
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//调用set方法
HelloSpring helloSpring = (HelloSpring) beanFactory.getBean("helloSpring");
//调用get方法
System.out.println(helloSpring.getName());
}
}
Spring-装配Bean的3种方式-默认构造函数方式装配Bean
原创
©著作权归作者所有:来自51CTO博客作者素颜猪的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java构造方法中怎么对字段初始化
java构造方法中怎么对字段初始化
构造方法 字段 初始化 -
【Spring Boot 源码学习】BootstrapRegistry 初始化器实现
本篇 Huazie 介绍了如何自定义 BootstrapRegistry 初始化器实现,其中演示如何在引导上下文中注册了自定义的对象以及如何在引导上下文中添加引导上下文关闭事件监听器。
Spring Boot 源码学习 BootstrapRegistry 初始化器实现 -
构造函数初始化Java 构造函数初始化顺序
在 B 类中,组合了一个 A 类对象,其中A类设计了构造函数。由于构造函数的调用规则,设计了构造函数就必须调用,但在定义B类时没有机会初始化A,因此采用构造函数的初始化列表来解决。
构造函数初始化Java 构造函数 初始化列表 析构函数 -
构造函数初始化
在C#中实例一个对象的时候执行一次构造函数初始化对象的数据
初始化 构造函数 数据 c#