基于注解方式实现属性注入
(1)@AutoWired:根据属性类型进行自动装配
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
第二步 在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解
package com.atguigu.spring.dao;public interface UserDao {public void add();
}
package com.atguigu.spring.dao;import org.springframework.stereotype.Repository;@Repository
public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add");}
}
package com.atguigu.spring.service;import com.atguigu.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Service(value = "userService")//<bean id = "" class = ""/>
public class UserService {//定义dao类型属性//不需要添加set方法//添加注入属性注解@Autowired //根据类型进行注入private UserDao userDao;public void add(){System.out.println("service add");userDao.add();}}
package com.atguigu.spring.testdemo;import com.atguigu.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void testService(){ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();}
}
(2)@Qualifier:根据属性名称进行注入
这个@Qualifier注解的使用,和上面@Autowired一起使用
package com.atguigu.spring.dao;import org.springframework.stereotype.Repository;@Repository(value = "userDaoImpl")
public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add");}
}
package com.atguigu.spring.service;import com.atguigu.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Service(value = "userService")//<bean id = "" class = ""/>
public class UserService {//定义dao类型属性//不需要添加set方法//添加注入属性注解@Autowired //根据类型进行注入@Qualifier (value = "userDaoImpl")private UserDao userDao;public void add(){System.out.println("service add");userDao.add();}}
(3)@Resource:可以根据类型注入,可以根据名称注入
@Resource//根据类型进行注入
@Resource(name = "userDaoImpl")//根据名称进行注入
(4)@Value:注入普通类型属性
package com.atguigu.spring.service;import com.atguigu.spring.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;//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Service(value = "userService")//<bean id = "" class = ""/>
public class UserService {@Value(value = "abc")private String name;//定义dao类型属性//不需要添加set方法//添加注入属性注解@Autowired //根据类型进行注入@Qualifier (value = "userDaoImpl")private UserDao userDao;public void add(){System.out.println("service add"+name);userDao.add();}}