spring Ioc(控制反转(Inversion of Control,缩写为IoC)),spring中可注入bean。
实例一、
使用下列加载:
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
Test2 t= (Test2)context.getBean("test2");
System.out.println(t.getName());
1 devSync.properties
name=zengyanhui
age=12
2 Test2
package test;
//public class Test1 extends InteractiveService {
public class Test2{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
3 spring-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="test.*" />
<context:property-placeholder location="classpath:/devSync.properties" />
<bean id="test2" name="test2" class="test.Test2">
<property name="name" value="${name}" />
<property name="age" value="${age}" />
</bean>
</beans>
实例二、
core.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Properties>xxxx.properties</Properties>
<Param name="app.name">dev-sync</Param>
<Param name="core.classpath.export">true</Param>
<Param name="core.queueSize">-1</Param>
<Param name="threadPool.coreSize">2</Param>
<Param name="threadPool.maxSize">200</Param>
<Param name="threadPool.keepAlive">10</Param>
<!--<Param name="http.proxy" export="true">${http.proxy:}</Param>
<Param name="http.proxy.bypass" export="true">${http.proxy.bypass:}</Param>-->
<ClassPath>lib/*.jar</ClassPath>
<!-- <Import>cn.cmri.koudai.push.processes.*</Import> -->
<Factory class="SpringBeanFactory" name="springFactory" config="spring-config.xml"/>
.....
<Include>integration.xml</Include>
</Context>
integration.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Include>xx-core.xml</Include>
</Context>
xx-core.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Import>xx.domain.*</Import>
<Import>xx.process.*</Import>
<Import>xx.util.*</Import>
<Service name="test3" class="Test3" event="Test3_Sms3Message">
<name>${name}</name>
<age>${age}</age>
</Service>
<Task name="projectUpdateByID33" event="jmsProjectUpdate33">
<CopyUpdate3 test3="[]"/>
<DispatchAll />
</Task>
</Context>
CopyUpdate3Proc
public class CopyUpdate3Proc extends DataProcess {
private static Logger log = Logger.getLogger(CopyUpdate3Proc.class);
private Test3 test3;
public void setTest3(Test3 test3) { //此设置值,为依赖注入
this.test3 = test3;
}
@Override
public boolean execute(ISession session) throws Exception { // 需要触发event:ProjectUpdatedTest
try{
System.out.println( test3.getName());
return false;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
spring-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 第二种方式是使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值 -->
<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:devSync.properties</value>
</array>
</property>
</bean>
</beans>
380

被折叠的 条评论
为什么被折叠?



