1.第一步导入注解的jar包
2.有三种装配方式
首先需要配置xml文件的beans
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
</beans>
1.使用context命名空间,通知spring扫描制定包下所有的bean类,进行注解解析
<context:component-scan base-package="com.lin.annotation" />
- 自动装配
使用自动装配需要添加setter方法
<bean id="userDao" class="com.lin.annotation.UserDaoImpl"></bean>
<bean id="userService" class="com.lin.annotation.UserServiceImpl" autowire="byName"></bean>
<bean id="userController" class="com.lin.annotation.UserController" autowire="byName"></bean>
- 使用context命名空间,在配置文件开启相应的注解处理器
<context:annotation-config></context:annotation-config>
<!-- 分别定义3个Bean实例 -->
<bean id="userDao" class="com.lin.annotation.UserDaoImpl"></bean>
<bean id="userService" class="com.lin.annotation.UserServiceImpl"></bean>
<bean id="userController" class="com.lin.annotation.UserController"></bean>
完整xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--spring-beans-4.3这里的4.3是指明版本号 -->
<!-- ===================================================== -->
<!-- 使用context命名空间,通知spring扫描制定包下所有的bean类,进行注解解析 -->
<!-- <context:component-scan base-package="com.lin.annotation" /> -->
<!-- ===================================================== -->
<!-- 使用context命名空间,在配置文件开启相应的注解处理器 -->
<!-- <context:annotation-config></context:annotation-config> -->
<!-- 分别定义3个Bean实例 -->
<!-- <bean id="userDao" class="com.lin.annotation.UserDaoImpl"></bean>
<bean id="userService" class="com.lin.annotation.UserServiceImpl"></bean>
<bean id="userController" class="com.lin.annotation.UserController"></bean>
-->
<!-- 自动装配 -->
<bean id="userDao" class="com.lin.annotation.UserDaoImpl"></bean>
<bean id="userService" class="com.lin.annotation.UserServiceImpl" autowire="byName"></bean>
<bean id="userController" class="com.lin.annotation.UserController" autowire="byName"></bean>
</beans>
各个类的代码
1.AnnotationAssembleTest 测试类,程序入口
package com.lin.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationAssembleTest {
public static void main(String[] args) {
//定义配置文件路径
String xmlPath="com/lin/annotation/beans6.xml";
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
//获取UserController实例
UserController userController= (UserController) applicationContext.getBean("userController");
//调用UserController中的save()方法
userController.save();
}
}
UserDao接口
package com.lin.annotation;
public interface UserDao {
public void save();
}
UserDaoImpl实现类
package com.lin.annotation;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("userDao....save..");
}
}
UserService接口
package com.lin.annotation;
public interface UserService {
public void save();
}
UserServiceImpl实现类
package com.lin.annotation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
//第一种注解
//@Resource(name = "userDao")
//第二种注解
@Autowired
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
this.userDao.save();//调用UserDao中的save()方法
System.out.println("userService...save...");
}
}
UserController类
package com.lin.annotation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller("userController") //注解就相当于配解了bean
public class UserController {
//@Resource(name = "userService")
@Autowired
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void save() {
this.userService.save();
System.out.println("userController...save...");
}
}