• Spring随笔 —— IOC配置的三种不同方式简介


      在spring framework中,IOC的配置是最基础的部分,常见的配置方式有基于xml文件和基于注解的配置方式。除了这两种配置方式之外,今天这里再介绍另一种配置方式,先用小demo重温下我们熟悉的两种配置方式。

      UserService调用UserDao中的login(String username, String password)方法。

      一、基于XML配置文件的方式

        UserDao.java

    1 package org.lizf.demo;
    2 
    3 public class UserDao {
    4 
    5     public void login(String username, String password) {
    6         System.out.println("login : " + username);
    7     }
    8     
    9 }

        UserService.java

     1 package org.lizf.demo;
     2 
     3 public class UserService {
     4     
     5     private UserDao userDao;
     6     
     7     public void login(String username, String password) {
     8         userDao.login(username, password);
     9     }
    10 
    11     public void setUserDao(UserDao userDao) {
    12         this.userDao = userDao;
    13     }
    14     
    15 }

        spring-context.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5     
     6     <bean id="userDao" class="org.lizf.demo.UserDao" />
     7     
     8     <bean id="userService" class="org.lizf.demo.UserService" >
     9         <property name="userDao" ref="userDao" />
    10     </bean>
    11 
    12 </beans>

        Main.java测试类:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7 
     8     public static void main(String[] args) {
     9         ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    10         UserService userService = (UserService) context.getBean("userService");
    11         userService.login("admin", "****");
    12     }
    13 
    14 }

        执行结果:

    七月 19, 2016 1:28:17 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5010be6: startup date [Tue Jul 19 01:28:17 CST 2016]; root of context hierarchy
    七月 19, 2016 1:28:17 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-context.xml]
    login : admin

      二、基于注解的配置方式

        UserDao.java:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 @Repository
     6 public class UserDao {
     7 
     8     public void login(String username, String password) {
     9         System.out.println("login : " + username);
    10     }
    11     
    12 }

        UserService.java:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Repository;
     5 
     6 @Service
     7 public class UserService {
     8     
     9     @Autowired
    10     private UserDao userDao;
    11     
    12     public void login(String username, String password) {
    13         userDao.login(username, password);
    14     }
    15 
    16 }

        spring-context.xml配置扫描包:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
     7     
     8     <context:component-scan base-package="org.lizf.demo" />
     9     
    10 
    11 </beans>

        Main.java测试类:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7 
     8     public static void main(String[] args) {
     9         ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    10         UserService userService = (UserService) context.getBean("userService");
    11         userService.login("admin", "****");
    12     }
    13 
    14 }

        输出结果:

    七月 19, 2016 1:40:40 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5010be6: startup date [Tue Jul 19 01:40:40 CST 2016]; root of context hierarchy
    七月 19, 2016 1:40:40 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-context.xml]
    login : admin

      三、基于Java类的配置方式

        UserDao.java:

    1 package org.lizf.demo;
    2 
    3 public class UserDao {
    4 
    5     public void login(String username, String password) {
    6         System.out.println("login : " + username);
    7     }
    8     
    9 }

        UserService.java:

     1 package org.lizf.demo;
     2 
     3 public class UserService {
     4     
     5     private UserDao userDao;
     6     
     7     public void login(String username, String password) {
     8         userDao.login(username, password);
     9     }
    10     
    11     public void setUserDao(UserDao userDao) {
    12         this.userDao = userDao;
    13     }
    14 
    15 }

        SpringConfig.java 配置类:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 
     6 //标注这是一个配置类
     7 @Configuration
     8 public class SpringConfig {
     9     
    10     //标注这是一个Bean
    11     @Bean
    12     public UserDao userDao() {
    13         return new UserDao();
    14     }
    15     
    16     @Bean
    17     public UserService userService() {
    18         UserService userService = new UserService();
    19         UserDao userDao = userDao();
    20         userService.setUserDao(userDao);
    21         return userService;
    22     }
    23     
    24 }

        Main.java测试类:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         //这里使用AnnotationConfigApplicationContext来创建ApplicationContext
     9         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    10         UserService userService = (UserService) context.getBean("userService");
    11         userService.login("admin", "****");
    12     }
    13 }

        输出结果:

    七月 19, 2016 1:53:55 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@179d3b25: startup date [Tue Jul 19 01:53:55 CST 2016]; root of context hierarchy
    login : admin

        如果需要对多个Config类进行加载,可以使用如下方式:

     1 package org.lizf.demo;
     2 
     3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     4 
     5 public class Main {
     6     public static void main(String[] args) {
     7         //如果有多个Config类,可以用下边的方式初始化
     8         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
     9         //regiester方法接受可变参数,传入多个Config类
    10         ctx.register(SpringConfig.class, SpringConfig2.class, SpringConfig3.class);
    11         //刷新ApplicationContext使配置生效
    12         ctx.refresh();
    13     }
    14 }
  • 相关阅读:
    单例模式
    面向对象编程(一)
    杨辉三角形
    静态方法,Arrays类,二维数组
    数组,查找算法,二分查找法,冒泡排序,选择排序,插入排序
    万年历(二)
    循环结构
    万年历(一)
    条件结构
    类型转换,位运算符
  • 原文地址:https://www.cnblogs.com/zfLee/p/5683291.html
Copyright © 2020-2023  润新知