1. 程式人生 > >Spring 註解<context:annotation-config> 和 <context:component-scan>的作用與區別

Spring 註解<context:annotation-config> 和 <context:component-scan>的作用與區別

poi dao lis 運行 ima scrip support .... res

<context:annotation-config> 是用於激活那些已經在spring容器裏註冊過的bean(無論是通過xml的方式還是通過packagesanning的方式)上面的註解。(激活@Resource和@Autowired註解)

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>還可以在指定的package下掃描以及註冊javabean 。(激活@Resource和@Autowired註解,同時可以配置掃描的包以激活@Service、@Controller等註解)

1.<context:component-scan>的作用:(開發中用這一個足以)

  一方面可以配置掃描註解的包的路徑,另一方面具有<context:annotation-config>的作用,也就是可以實現註解註入(Autowired與Resource等註解)。

例如:

目錄結構:

    技術分享圖片

ApplicationContext-test.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:context="http://www.springframework.org/schema/context" 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"
> <!--配置掃描註解的包--> <context:component-scan base-package="cn.qlq.test" ></context:component-scan> </beans>

package cn.qlq.test.dao;

import org.springframework.stereotype.Repository;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:49 2018/9/28
 */
@Repository
public class UserDao {
    public void saveUser() {
        System.out.println("save user");
    }
}

package cn.qlq.test.service;

import cn.qlq.test.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:52 2018/9/28
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public void saveUser() {
        userDao.saveUser();
    }
}

測試類:

package cn.qlq.test;

import cn.qlq.test.service.UserService;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:54 2018/9/28
 */
public class TestApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext-test.xml");
        //第一種:用beanId獲取bean
//        UserService userService = (UserService) applicationContext.getBean(UserService.class);
        //第二種:用bean的class獲取bean
        UserService userService = (UserService) applicationContext.getBean(UserService.class);
        userService.saveUser();
    }
}

結果:

save user

補充:

  <context:component-scan....可以掃描多個包,逗號隔開就行,而且掃描包的時候會自動掃描子包。如下也是正確的:

<?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"
       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">
    <!--配置掃描註解的包-->
    <context:component-scan base-package="cn.qlq.test.dao,cn.qlq.test.service" ></context:component-scan>
</beans>

2.<context:annotation-config>的作用與測試

  <context:annotation-config> 是用於激活那些已經在spring容器裏註冊過的bean(無論是通過xml的方式還是通過package sanning的方式)上面的註解。也就是激活@Autowired和@Resource註解。但是不會掃描@Service等註解配置的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"
       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">
    <context:annotation-config/>

    <bean id="userDao" class="cn.qlq.test.dao.UserDao"/>
    <bean id="userService" class="cn.qlq.test.service.UserService"/>
</beans>

package cn.qlq.test.dao;

import org.springframework.stereotype.Repository;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:49 2018/9/28
 */
public class UserDao {
    public void saveUser() {
        System.out.println("save user");
    }
}

package cn.qlq.test.service;

import cn.qlq.test.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: qlq
 * @Description
 * @Date: 22:52 2018/9/28
 */
public class UserService {
    @Autowired
    private UserDao userDao;

    public void saveUser() {
        userDao.saveUser();
    }
}

測試代碼同上,可以正常運行。

如果我們修改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"
       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">
    <!--配置掃描註解的包-->
    <context:annotation-config/>
</beans>

結果報錯沒有找到bean:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.qlq.test.service.UserService] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:972)
    at cn.qlq.test.TestApp.main(TestApp.java:17)

修改xml為如下:(去掉annotation-config標簽)

<?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"
       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">
    <bean id="userDao" class="cn.qlq.test.dao.UserDao"/>
    <bean id="userService" class="cn.qlq.test.service.UserService"/>
</beans>

結果會報空指針異常,service中沒有註入dao:

Exception in thread "main" java.lang.NullPointerException
    at cn.qlq.test.service.UserService.saveUser(UserService.java:17)
    at cn.qlq.test.TestApp.main(TestApp.java:18)

Spring 註解<context:annotation-config> 和 <context:component-scan>的作用與區別