三:springIOC

结构
pom.xml文件中                                                              
                                                                
<?xml version="1.0" encoding="UTF-8" ?>                                                             
<project xmlns=…...........                                                             
    <modelVersion>4.0.0<.modelVersion>                                                          
                                                                
    <groupId>cn.xh.spring</groupId>                                                         
    <artifactId>xh_spring01</artifactId>                                                            
    <version>1.0-SNAPSHOT</version>                                                         
                                                                
    //打包                                                            
    <packaging>jar</packaging>                                                          
    //引入依赖,引入好了发现External Libraries里导入了很多包(aop,beans,context,core)                                                          
    <dependencies>                                                          
        <dependency>                                                        
            <groupId>org.springframework</groupId>                                                  
            <artifactId>spring-context</artifactId>                                                 
            <version>5.0.2.RELEASE</version>                                                    
        </dependency>                                                       
    </dependencies>                                                         
</project>                                                              
                                                                
                                                                
                                                                
beans.xml文件中                                                                
//绿色部分每次直接粘贴过来用即可,无需修改                                                              
<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                                                            
                        http://www.springframework.org/schema/util                                      
                        http://www.springframework.org/schema/util/spring-util-4.0.xsd"                                     
                        >                                       
    //进行配置,这里的id相当于案例sheet里的bean.properties里的key,class相当于key对应的value                                                            
    <bean id="accountDao" class="cn.xh.dao.impl.AccountDaoMybatisImpl"></bean>                                                          
</beans>                                                                
                                                                
                                                                
IAccountDao.java文件中                                                             -
                                                                
public interface IAccountDao {                                                              
    void saveAccout();                                                          
}                                                               
                                                                
                                                                
AccountDaoImpl.java文件中                                                              
public class AccountDaoImpl implements IAccountDao {                                                                
    @Override                                                           
    public void saveAccount() {                                                         
        System.out.println("jdbc保存账户!");                                                        
    }                                                           
}                                                               
                                                                
AccountDaoMybatisImpl.java文件中                                                               
public class AccountDaoMybatisImpl implements IAccountDao {                                                             
    @Override                                                           
    public void saveAccount() {                                                         
        System.out.println("Mybatis保存账户!");                                                     
    }                                                           
}                                                               
                                                                
                                                                
client.java文件中                                                              
                                                                
public class client {                                                               
    public static void main(String[] args) {                                                            
                                                                
        //创建一个工厂方法1,BeanFactory是在获取对象(getBean())的时候才创建对象                                                        
        ClassPathResource resource = new ClassPathResource("beans.xml");                                                        
        BeanFactory context = new XmlBeanFactory(resource);                                                     
                                                                
        //创建一个工厂方法2,ApplicationContext是Spring封装好的类。是在加载beans.xml配置文件的时候创建的对象                                                        
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");                                                       
                                                                
        //创建一个工厂方法3,参数要写bean.xml文件的绝对路径,这个文件放到哪里都可以                                                     
        ApplicationContext context = new FileSystemXmlApplicationContext("C:\\Users\\admin\\Desktop\\bean.xml");                                                        
                                                                
                                                                
        //通过工厂获取对象(getBean()是自带的方法,获取到参数对应的类对象)                                                     
        IAccountDao accountDao = (IAccountDao)context.getBean("accountDao");                                                        
        accountDao.saveAccount();                                                       
        //运行结果为在控制台打印出:mybatis保存账户!                                                     
    }                                                           
}                                                               
                                                                
//想要执行结果为打印jdbc保存账户!,只需要将beans.xml文件里的内容修改为:                                                                
<bean id="accountDao" class="cn.xh.dao.impl.AccountDaoImpl"></bean>                                                             

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容