关于Java中Spring IOC的配置问题
1个回答
展开全部
.java代码:
package ioc.first;
import ioc.first.service.StoreService;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// FileSystemResource("D:/a.txt");
// Resource resource=new ClassPathResource("ioc/first/applicationContext.xml");
// BeanFactory context=new XmlBeanFactory(resource);
ApplicationContext context=new ClassPathXmlApplicationContext("ioc/first/applicationContext_autowire.xml");
StoreService service=(StoreService)context.getBean("storeService") ;
service.submitOrder();
// Map env=(Map)context.getBean("env");
// System.out.println(env.get("JAVA_HOME"));
//
// System.out.println(context.getBean("env2"));
}
}
applicationContext_autowire.xml 代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"
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-2.0.xsd">
<bean id="itemDao" class="ioc.first.dao.ItemDaoImpl"/>
<bean id="orderDao" class="ioc.first.dao.OrderDaoImpl"/>
<bean id="oDao1" class="ioc.first.dao.OrderDaoImpl"/>
<bean id="storeService"
class="ioc.first.service.StoreServiceImpl"
autowire="autodetect">
</bean>
</beans>
上面java代码中的//后面是注释。可以不用管 是我用来做测试的.
package ioc.first;
import ioc.first.service.StoreService;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// FileSystemResource("D:/a.txt");
// Resource resource=new ClassPathResource("ioc/first/applicationContext.xml");
// BeanFactory context=new XmlBeanFactory(resource);
ApplicationContext context=new ClassPathXmlApplicationContext("ioc/first/applicationContext_autowire.xml");
StoreService service=(StoreService)context.getBean("storeService") ;
service.submitOrder();
// Map env=(Map)context.getBean("env");
// System.out.println(env.get("JAVA_HOME"));
//
// System.out.println(context.getBean("env2"));
}
}
applicationContext_autowire.xml 代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"
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-2.0.xsd">
<bean id="itemDao" class="ioc.first.dao.ItemDaoImpl"/>
<bean id="orderDao" class="ioc.first.dao.OrderDaoImpl"/>
<bean id="oDao1" class="ioc.first.dao.OrderDaoImpl"/>
<bean id="storeService"
class="ioc.first.service.StoreServiceImpl"
autowire="autodetect">
</bean>
</beans>
上面java代码中的//后面是注释。可以不用管 是我用来做测试的.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询