class MainModule{
private DependModuleA moduleA;
private DependModuleB moduleB;
public DependModuleA getModuleA() {
return moduleA;
}
public void setModuleA(DependModuleA moduleA) {
this.moduleA = moduleA;
}
public DependModuleB getModuleB() {
return moduleB;
}
public void setModuleB(DependModuleB moduleB) {
this.moduleB = moduleB;
}
}
interface DependModuleA{
public void funcFromModuleA();
}
interface DependModuleB{
public void funcFromModuleB();
}
class DependModuleAImpl implements DependModuleA{
@Override
public void funcFromModuleA() {
System.out.println("This is func from Module A");
}
}
class DependModuleBImpl implements DependModuleB{
@Override
public void funcFromModuleB() {
System.out.println("This is func from Module B");
}
}
public class SimpleIOCDemo {
public static void main(String[] args) throws ClassNotFoundException {
MainModule mainModule = new MainModule();
mainModule.setModuleA(new DependModuleAImpl());
mainModule.setModuleB(new DependModuleBImpl());
mainModule.getModuleA().funcFromModuleA();
mainModule.getModuleB().funcFromModuleB();
}
}
class SimpleIOCContainer{
private Properties properties = new Properties();
private Map<String, Object> moduleMap = new HashMap<>();
{
try {
properties.load(new FileInputStream(new File("SimpleIOC.properties")));
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String moduleName) throws ClassNotFoundException {
Object instanceObj;
if(moduleMap.get(moduleName)!=null){
System.out.println("return old bean");
return moduleMap.get(moduleName);
}
System.out.println("create new bean");
String fullClassName = properties.getProperty(moduleName);
if(fullClassName == null)
throw new ClassNotFoundException();
else{
Class<? extends Object> clazz = Class.forName(fullClassName);
try {
instanceObj = clazz.newInstance();
instanceObj = buildAttachedModules(moduleName,instanceObj);
moduleMap.put(moduleName, instanceObj);
return instanceObj;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
private Object buildAttachedModules(String modulename , Object instanceObj) {
Set<String> propertiesKeys = properties.stringPropertyNames();
Field[] fields = instanceObj.getClass().getDeclaredFields();
for (String key : propertiesKeys) {
if(key.contains(modulename)&&!key.equals(modulename)){
try {
Class<? extends Object> clazz = Class.forName(properties.getProperty(properties.getProperty(key)));
for (Field field : fields) {
if(field.getType().isAssignableFrom(clazz))
field.set(instanceObj, clazz.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return instanceObj;
}
}
mainModule=com.rocking.demo.MainModule mainModule.moduleA=moduleA mainModule.moduleB=moduleB moduleA=com.rocking.demo.DependModuleAImpl moduleB=com.rocking.demo.DependModuleBImpl
public class SimpleIOCDemo {
public static void main(String[] args) throws ClassNotFoundException {
SimpleIOCContainer container = new SimpleIOCContainer();
DependModuleA moduleA = (DependModuleA) container.getBean("moduleA");
moduleA.funcFromModuleA();
DependModuleB moduleB = (DependModuleB) container.getBean("moduleB");
moduleB.funcFromModuleB();
MainModule mainModule = (MainModule) container.getBean("mainModule");
mainModule.getModuleA().funcFromModuleA();
mainModule.getModuleB().funcFromModuleB();
container.getBean("mainModule");
}
}
<?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-3.0.xsd">
<bean id="mainModule" class="com.rocking.demo.MainModule">
<property name="moduleA">
<ref bean="moduleA"/>
</property>
<property name="moduleB">
<ref bean="moduleB"/>
</property>
</bean>
<bean id="moduleA" class="com.rocking.demo.DependModuleAImpl"></bean>
<bean id="moduleB" class="com.rocking.demo.DependModuleBImpl"></bean>
</beans>
class MainModule {
private DependModuleA moduleA;
private DependModuleB moduleB;
public DependModuleA getModuleA() {
return moduleA;
}
public void setModuleA(DependModuleA moduleA) {
this.moduleA = moduleA;
}
public DependModuleB getModuleB() {
return moduleB;
}
public void setModuleB(DependModuleB moduleB) {
this.moduleB = moduleB;
}
}
interface DependModuleA {
public void funcFromModuleA();
}
interface DependModuleB {
public void funcFromModuleB();
}
class DependModuleAImpl implements DependModuleA {
@Override
public void funcFromModuleA() {
System.out.println("This is func from Module A");
}
}
class DependModuleBImpl implements DependModuleB {
@Override
public void funcFromModuleB() {
System.out.println("This is func from Module B");
}
}
public class SimpleIOCDemo {
public static void main(String[] args) throws ClassNotFoundException {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions("Beans.xml");
MainModule mainModule = (MainModule) beanFactory.getBean("mainModule");
mainModule.getModuleA().funcFromModuleA();
mainModule.getModuleB().funcFromModuleB();
}
}
public class SimpleIOCDemo {
public static void main(String[] args) throws ClassNotFoundException {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
AbstractBeanDefinition mainModule = new RootBeanDefinition(MainModule.class);
AbstractBeanDefinition moduleA = new RootBeanDefinition(DependModuleAImpl.class);
AbstractBeanDefinition moduleB = new RootBeanDefinition(DependModuleBImpl.class);
beanFactory.registerBeanDefinition("mainModule", mainModule);
beanFactory.registerBeanDefinition("moduleA", moduleA);
beanFactory.registerBeanDefinition("moduleB", moduleB);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("moduleA", moduleA);
propertyValues.add("moduleB", moduleB);
mainModule.setPropertyValues(propertyValues);
MainModule module = (MainModule) beanFactory.getBean("mainModule");
module.getModuleA().funcFromModuleA();
module.getModuleB().funcFromModuleB();
}
}
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions( new ClassPathResource( "Beans.xml")); PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); configurer.setLocation( new ClassPathResource( "about.properties")); configurer.postProcessBeanFactory( beanFactory);
BeanWrapper beanWrapper = new BeanWrapperImpl(Class.forName("com.rocking.demo.MainModule"));
beanWrapper.setPropertyValue( "moduleA", Class.forName("com.rocking.demo.DepModuleAImpl").newInstance());
beanWrapper.setPropertyValue( "moduleB", Class.forName("com.rocking.demo.DepModuleBImpl").newInstance());
MainModule mainModule= (MainModule) beanWrapper.getWrappedInstance();
mainModule.getModuleA().funcFromA();
mainModule.getModuleB().funcFromB();
class ModuleC {
private String x;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
}
class ModulePostProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object object, String string)
throws BeansException {
System.out.println(string);
if(object instanceof ModuleC){
System.out.println(string);
((ModuleC)object).setX("after");
}
return object;
}
@Override
public Object postProcessBeforeInitialization(Object object, String string)
throws BeansException {
if(object instanceof ModuleC){
((ModuleC)object).setX("before");
}
return object;
}
}
public class VerySimpleIOCKernal {
public static void main(String[] args) throws ClassNotFoundException, BeansException, InstantiationException, IllegalAccessException {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("Beans.xml"));
ModulePostProcessor postProcessor = new ModulePostProcessor();
beanFactory.addBeanPostProcessor(postProcessor);
MainModule module = (MainModule) beanFactory.getBean("mainModule");
ModuleC moduleC = (ModuleC) beanFactory.getBean("moduleC");
System.out.println(moduleC.getX());
}
}
<?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="mainModule" class="com.rocking.demo.MainModule">
<property name="moduleA">
<ref bean="moduleA"/>
</property>
<property name="moduleB">
<ref bean="moduleB"/>
</property>
</bean>
<bean id="moduleA" class="com.rocking.demo.DepModuleAImpl">
<property name="infoA">
<value>${moduleA.infoA}</value>
</property>
</bean>
<bean id="moduleB" class="com.rocking.demo.DepModuleBImpl">
<property name="infoB">
<value>info of moduleB</value>
</property>
</bean>
<bean id="moduleC" class="com.rocking.demo.ModuleC">
</bean>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有