我们使用xml-Bean标签的配置方式和注解做对比理解
1.创建UserDao接口以及UserDao的实现类UserDaoImpl(接口代码省略)
public class UserDaoImpl implements UserDao {@Override
public void save1() {
System.out.println("save running...");
}
}
2.创建UserService接口以及UserServiceImpl实现类(接口代码省略)
public class UserServiceImpl implements UserService {private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void sava() {
userDao.save1();
}
}
3.配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.hao.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.hao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
如果注解中有不明白的请访问我的博客中的依赖注入分析(xml配置)
4.模拟web端进行测试
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userService");
service.sava();
}
}
结果:save running…
===============================================================================================================================================================================================================================================================
然后实现注解进行操作
1.创建UserDao接口及其实现类UserDaoImpl(接口代码省略)
@Component("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void save1() {
System.out.println("save running...");
}
}
注:注释掉的bean标签的内容相当于下面的表签
2.创建UserService接口及其实现类UserServiceImpl(接口代码省略)
//<bean id="userService" class="com.hao.service.impl.UserServiceImpl">@Component("userService")
public class UserServiceImpl implements UserService {
// <property name="userDao" ref="userDao"/>
@Autowired
@Qualifier("userDao") //要注入的id值
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void sava() {
userDao.save1();
}
}
3.进行web层模拟测试
public class UserController {public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userService");
service.sava();
}
}
结果:
然后发现报错了,查看错误原因:发现是我们虽然使用了注解进行配置,但是没有告诉spring去哪里找注解,所以造成了没有创建实例
#配置组件扫描l告诉spring在哪个包下及其子包下的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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置组件扫描-->
<!-- spring会扫描hao包下的所有子包和该包下的所有类-->
<context:component-scan base-package="com.hao"/>
</beans>
再此测试:
结果:save running…
补充:思考我们在类前使用@Component注解,并不能分辨这是哪一层上的(dao层,servce层,controller层),所以就引入了另外三个标签进行分辨,请访问spring原始注解开发-02查看内容