Spring IOC XML Configuration

[align=center][b]Spring Note[/b][/align]
[b]Spring Introdution
引入相关的包[/b]
[list]
[*]----spring-context-3.2.1.RELEASE.jar
[*]----spring-beans-3.2.1.RELEASE.jar
[*]----spring-core-3.2.1.RELEASE.jar
[*]----spring-expression-3.2.1.RELEASE.jar
[*]----commons-logging-1.1.2.jar
[/list]
[b]UserDAOImpl类:[/b]
package com.edu.hpu.impl;

import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.User;

public class UserDAOImpl implements UserDAO {

public void save(User user) {
System.out.println("user saved!");
}

}

[b]UserService类:[/b]
package com.edu.hpu.service;

import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.User;

public class UserService {

private UserDAO userDAO;

public void save(User user) {
userDAO.save(user);
}

public UserDAO getUserDAO() {
return userDAO;
}

public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}

[b]beans.xml配置文件:[/b]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 id="u" class="com.edu.hpu.impl.UserDAOImpl"></bean>

<bean id="userService" class="com.edu.hpu.service.UserService">
<property name="userDAO" ref="u" />
</bean>
</beans>

[b]测试类:[/b]
package com.edu.hpu.sevice;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.edu.hpu.model.User;
import com.edu.hpu.service.UserService;

public class TestUser {

@Test
public void testSave() {
ApplicationContext acx = new ClassPathXmlApplicationContext("
beans.xml");

UserService service = (UserService)acx.getBean("userService");
User u = new User();
service.save(u);
}

}

[b]注入的方式大致有三种,上面是Setter Injection[/b]
[list]
[*]----Setter Injection(重要)
[*]----Constructor Injection(不重要)
[*]----Interface Injection(可以忘记)
[/list]
下面以Constructor Injection为例子进行注入:
[b]给UserService类添加Constructor方法:[/b]
public UserService(UserDAO userDAO) {
super();
this.userDAO = userDAO;
}

[b]在beans.xml里面配置Constructor Injection:[/b]
<bean id="userService" class="com.edu.hpu.service.UserService">
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>

[color=red][b]配置bean的时候,属性名称id也可以使用name代替,但是name里面不能加入特殊字符[/b][/color]
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl"></bean>

[b]简单属性的注入[/b]
在UserDAOImpl类里面添加属性daoID , daoStatus,并添加相应的getter,setter 方法
在beans.xml里面进行简单属性的配置:
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl">
<property name="daoID" value="1" />
<property name="daoStatus" value="run" />
</bean>

[b]bean的生存范围[/b]
[color=red]the Spring Framework supports five scopes, three of which are available only if you use a web-aware ApplicationContext.[/color]
[b]scope : singleton,prototype,request,session,global session[/b]
singleton是默认的,而后面的三种,只有使用web-aware Application时才会起作用
<bean id="userService" class="com.hpu.service.UserService" scope="prototype">

[b]集合注入(Collection Injection)[/b]
在UserDAOImpl类里面添加三个集合属性,并设定getter与setter方法:
[list]
[*]private List<String> lists;
[*]private Set<String> sets;
[*]private Map<String , String> maps;
[/list]
在beans.xml里面进行集合注入的配置:
<bean name="u" class="com.edu.hpu.impl.UserDAOImpl">
<property name="lists">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<property name="sets" >
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="maps">
<map>
<entry key="1" value="1"></entry>
<entry key="2" value="2"></entry>
<entry key="3" value="3"></entry>
</map>
</property>
</bean>

[b]自动装配(autowire)[/b]
[b]装配方式有四种:no , byName , byType , construtor[/b]
<bean id="userService" class="com.hpu.service.UserService" autowire="byName">
</bean> <!-- 找出beans.xml里面与该类里面属性名一样的bean进行装配 -->
<bean id="userService" class="com.hpu.service.UserService" autowire="byName">
</bean> <!-- 找出beans.xml里面与该类里面属性的类型一样的bean进行装配 -->

[b]bean的生命周期[/b]
在UserService类里面,加入init(),destroy()方法,在beans.xml里面配置如下:
<bean id="userService" class="com.edu.hpu.service.UserService" 
autowire="byType" init-method="init" destroy-method="destroy">
</bean>

[color=red]注意:init-method , destroy-method 不要和scope=”prototype”一起使用。[/color]
[color=red]本文所述十分简略,若要知道详细操作可参考spring所提供的官方文档。[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值