基于注解方式实现属性注入
(1)@Autowired: 根据属性类型进行自动装配
第一步把service和dao对象创建,在service和dao类添加创建对象注解
前提:注解扫描器开启
<context:component-scan base-package="com.atguigu.spring5"/>
package com.atguigu.spring5.dao;
public interface UserDao {
public void add();
}
package com.atguigu.spring5.dao;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add...........");
}
}
第二步在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//@Component(value = "userService") //<bean id="userService" class="..">
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired //根据类型进行注入
@Qualifier(value = "userDaoImpl1")//根据名称进行注入
private UserDao userDao;
public void add(){
System.out.println("service add.....");
userDao.add();
}
}
(2)@Qualifier: 根据属性名称进行注入
这个@Qualifier注解的使用,和上面@Autowired一起使用
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//@Component(value = "userService") //<bean id="userService" class="..">
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired //根据类型进行注入
@Qualifier(value = "userDaoImpl1")//根据名称进行注入
private UserDao userDao;
public void add(){
System.out.println("service add.....");
userDao.add();
}
}
(3)@Resource: 可以根据类型注入,可以根据名称注入
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//@Component(value = "userService") //<bean id="userService" class="..">
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
// @Autowired //根据类型进行注入
// @Qualifier(value = "userDaoImpl1")//根据名称进行注入
// private UserDao userDao;
//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1") //根据名称进行注解
private UserDao userDao;
public void add(){
System.out.println("service add.....");
userDao.add();
}
}
(4)@Value(): 注入普通类型属性
2.完全注解开发
(1)创建配置类,替代xml配置文件
package com.atguigu.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages ={"com.atguigu"} )
public class SpringConfig {
}
其他部分都一样
测试类不同
(2)编写测试类
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.config.SpringConfig;
import com.atguigu.spring5.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import javax.naming.Context;
public class TestSpring5Demo1 {
@Test
public void testService2(){
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}