Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别
2021-06-17 07:03
标签:.class rda port coding ini getbean 结构 utf-8 指定 一方面可以配置扫描注解的包的路径,另一方面具有 例如: 目录结构: ApplicationContext-test.xml 测试类: 结果: save user 补充: 如下: 测试代码同上,可以正常运行。 如果我们修改xml为下面:(去掉bean定义) 结果报错没有找到bean: 修改xml为如下:(去掉annotation-config标签) 结果会报空指针异常,service中没有注入dao: 标签:.class rda port coding ini getbean 结构 utf-8 指定 原文地址:https://www.cnblogs.com/qlqwjy/p/9721466.html1.
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();
}
}
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.
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 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>
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 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>
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>的作用与区别
文章链接:http://soscw.com/essay/94925.html