一、使用xml 生成bean
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描service包--> <context:component-scan base-package="com.java.SimpleFactory"></context:component-scan> <!--创建dbcp数据源连接池--> <!--创建mybatis的SqlSessionFactory工厂类对象--> <!--配置扫描mybatis的dao接口 ,会为dao接口创建myabtis的dao接口实现类对象,放置到session工厂中--> <!--声明spring 的事物管理器对象--> <!--声明以注解的方式配置spring 的事物--> <!--引入spring 和shiro集成的主配置文件--> </beans>
二、测试类
public class test { @Autowired Man man; public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException { /* Animal animal=(Animal) BeanFactory.getBean("man"); animal.say(); Animal animal2=(Animal) BeanFactory.getBean("man"); animal2.say(); *//* Car car=(Car) BeanFactory.getBean("care"); car.run();*//* System.out.println(animal==animal2);*/ ApplicationContext ctx = new FileSystemXmlApplicationContext("filename.xml"); Animal animal2=(Animal) ctx.getBean("man"); Animal animal3=(Animal) ctx.getBean("man"); animal2.say(); System.out.println(ctx); }