展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring5入门(十):基于注解管理bean,注入属性

  • @Autowired:根据属性类型进行自动装配
# bean.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"
       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="com.ychen"></context:component-scan>

</beans>

# 持久层接口
public interface UserDao {

    public void add();

}

# 持久层实现
@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void add() {
        System.out.println("dao add.....");
    }

}

# 业务层
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public void add() {
        System.out.println("service add.......");
        userDao.add();
    }

}

# 测试方法
public class Test3 {

    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

}

# 控制台
com.ychen.spring.ser.UserService@37911f88
service add.......
dao add.....

Process finished with exit code 0
  • @Qualifier:根据名称进行注入,需配合@Autowired一起使用
# bean.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"
       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="com.ychen"></context:component-scan>

</beans>

# 持久层接口
public interface UserDao {

    public void add();

}

# 持久层实现
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {

    @Override
    public void add() {
        System.out.println("dao add.....");
    }

}

# 业务层
@Service
public class UserService {

    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add() {
        System.out.println("service add.......");
        userDao.add();
    }

}

# 测试方法
public class Test3 {

    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

}

# 控制台
com.ychen.spring.ser.UserService@37911f88
service add.......
dao add.....

Process finished with exit code 0
  • @Resource:可以根据类型注入,可以根据名称注入,该注解并不时spring提供的注解,只是java扩展包中的注解
## 测试一:根据类型注入
# bean.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"
       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="com.ychen"></context:component-scan>

</beans>

# 持久层接口
public interface UserDao {

    public void add();

}

# 持久层实现
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {

    @Override
    public void add() {
        System.out.println("dao add.....");
    }

}

# 业务层
@Service
public class UserService {

    @Resource
    private UserDao userDao;

    public void add() {
        System.out.println("service add.......");
        userDao.add();
    }

}

# 测试方法
public class Test3 {

    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

}

# 控制台
com.ychen.spring.ser.UserService@37911f88
service add.......
dao add.....

Process finished with exit code 0

## 测试二:根据名称注入
# bean.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"
       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="com.ychen"></context:component-scan>

</beans>

# 持久层接口
public interface UserDao {

    public void add();

}

# 持久层实现
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {

    @Override
    public void add() {
        System.out.println("dao add.....");
    }

}

# 业务层
@Service
public class UserService {

    @Resource(name = "userDaoImpl1")
    private UserDao userDao;

    public void add() {
        System.out.println("service add.......");
        userDao.add();
    }

}

# 测试方法
public class Test3 {

    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

}

# 控制台
com.ychen.spring.ser.UserService@37911f88
service add.......
dao add.....

Process finished with exit code 0
  • @Value:注入普通类型属性
# bean.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"
       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="com.ychen"></context:component-scan>

</beans>

# 持久层接口
public interface UserDao {

    public void add();

}

# 持久层实现类
@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void add() {
        System.out.println("dao add.....");
    }

}

# 业务层
@Service
public class UserService {

    @Value(value = "abc")
    private String name;

    @Autowired
    private UserDao userDao;

    public void add() {
        System.out.println("service add......." + name);
        userDao.add();
    }

}

# 测试方法
public class Test3 {

    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

}

# 控制层
com.ychen.spring.ser.UserService@2cb2fc20
service add.......abc
dao add.....

Process finished with exit code 0
  • 纯注解开发
# 编写配置类
@Configuration
@ComponentScan(basePackages = {"com.ychen"})
public class SpringConfig {

}

# 编写测试方法
    @Test
    public void testService2() {
        ApplicationContext context
                = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }

# 控制台
com.ychen.spring.ser.UserService@7975d1d8
service add.......abc
dao add.....

Process finished with exit code 0
posted @ 2022-04-18 21:42  DogLeftover  阅读(30)  评论(0编辑  收藏  举报