[SXT][WY]Spring03 IoC容器

1、spring依赖库
    * SPRING_HOME/dist/spring.jar
    * SPRING_HOME/lib/jakarta-commons/commons-logging.jar
    * SPRING_HOME/lib/log4j/log4j-1.2.14.jar
   
2、拷贝spring配置文件到src下

3、拷贝log4j配置文件到src下

4、在UserManagerImpl中提供构造函数或setter方法,spring将实例化好的UserDao实现注入给我们

5、让spring管理我们的对象创建和依赖,必须在spring配置中进行定义

6、编写客户端


spring Ioc容器的关键点:
    * 必须将被管理的对象定义到spring配置文件中
    * 必须定义构造函数或setter方法,让spring将对象注入过来

 

applicationContext.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:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
          
    <bean id="userDao4MySqlImpl" class="com.bjsxt.spring.dao.UserDao4MySqlImpl"/>
    <bean id="userDao4OracleImpl" class="com.bjsxt.spring.dao.UserDao4OracleImpl"/>
   
    <bean id="userManager" class="com.bjsxt.spring.manager.UserManagerImpl">
       
        <!-- 构造方法注入
        <constructor-arg ref="userDao4OracleImpl"/>
         -->
         <property name="userDao" ref="userDao4OracleImpl"/>
    </bean>
   
</beans>

 

log4j.xml

 

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 

Client.java

 

public class Client {

    public static void main(String[] args) {
        //UserManager userManager = new UserManagerImpl(new UserDao4OracleImpl());
//        UserManager userManager = new UserManagerImpl(new UserDao4MySqlImpl());
//        userManager.save("张三", "123");
       
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
       
        UserManager userManager = (UserManager)factory.getBean("userManager");
        userManager.save("张三", "123");
       
//        UserManagerImpl userManager = new UserManagerImpl();
//        userManager.setUserDao(new UserDao4MySqlImpl());
//        userManager.save("张三", "123");
    }
}

 

你可能感兴趣的:(spring,AOP,log4j,bean,IOC)