Day21 SpringDI - 注解

通过上次学习后,我们可以做个小案例

xml配置

实体类

public class Person {
    
    
    private String username;
    private String password;
    //省略getset和构造
}

PersonDao

public class PersonDao {
    
    
    public Person find(Person person) {
    
    
        if("jack".equals(person.getUsername())&&"1234".equals(person.getPassword())){
    
    
            return person;
        }
        return null;
    }
}

PersonService

public class PersonService {
    
    
    //成员变量
    private PersonDao personDao;
    //set方法
    public void setPersonDao(PersonDao personDao) {
    
    
        this.personDao = personDao;
    }

    public boolean login(Person person) {
    
    
        Person person1= personDao.find(person);
        if (person1==null){
    
    
            return false;
        }else {
    
    
            return true;
        }
    }
}

applicationContext.xml

	<!--配置personService-->
    <bean id="personService" class="cn.cyl.service.PersonService">
        <property name="personDao" ref="personDao"/>
    </bean>
    <!--配置personDao-->
    <bean id="personDao" class="cn.cyl.dao.PersonDao"></bean>

	<!--配置person-->
	<bean id="person111" class="cn.cyl.domain.Person">
        <property name="username" value="jack"/>
        <property name="password" value="1234"/>
    </bean>

测试

	@Test
    public void test111(){
    
    
        Person person = (Person) context.getBean("person111");
        PersonService personService = (PersonService) context.getBean("personService");
        boolean flag = personService.login(person);//true
    }

这种配置代码较多,有一种更简洁的方法是用注解配置。

Spring依赖注入-注解创建对象***

  • (1)对象比较多的话,开启注解扫描。使用这种方法需要在applicationContext.xml中添加一些约束。

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"
       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">
    <!--
            使用注解方式进行创建对象
            1.开启注解扫描

            含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包中所有的类
            查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
            放到spring容器中
        -->
    <context:component-scan base-package="cn.cyl"/>
</beans>
  • (2)只有标记有注解的类,才会被创建对象且添加到ioc容器中
  • (3)四个注解
//@Component  //其他层
//@Repository //Dao层
//@Service    //Service层
@Controller("xxx")  //Web层 <bean id = "xxx" class="com.wzx.annotation.Student"/>
public class Student {
    
    
    //如果创建对象时,给对象赋值,则需要添加Value属性
    @Value("宝强")
    private String name;
    @Value("18")
    private int age;
    public void study(){
    
    
        System.out.println("学生学习!:name="+name);
    }
}
  • 注解没有配置id,但是默认是类名首字母小写
  • 示例

PersonDao

//相当于在xml中配置了一个bean标签
@Service
public class PersonService {
    
    
    //成员变量
    private PersonDao personDao;
}    

PersonService

@Repository
public class PersonDao {
    
    
}

测试

	@Test
    public void test222(){
    
    
        PersonService personService = (PersonService) context.getBean("personService");
        PersonDao personDao = (PersonDao) context.getBean("personDao");
    }

在这个测试中,PersonService对象中的成员变量PersonDao没有被赋值,还是null。
接下来就通过注解实现注入。

Spring依赖注入-注解实现注入***

  • (1)注入是什么?
    就是查找之后,进行赋值
  • (2)三种注入方式
  1. @Autowired
    或者
    @Autowired
    @Qualifier(“bean的id”)
  2. @Value("#{bean的id}")
  3. @Resource(name=“bean的id值”)
  • 示例

PersonDao

//相当于在xml中配置了一个bean标签
@Service
public class PersonService {
    
    
    //成员变量
    @Autowired
    private PersonDao personDao;
}    
  • 详细用法
/**
     * 第一种注入方式:
     * 注入UserDao的实现类
     * @Autowired 自动注入,写在类的成员变量上, 可以根据UserDao接口,创建该接口的实现类,在自动的设置到该成员变量上
     */
    @Autowired
    @Qualifier("userDaoImpl") //Qualifier 根据bean 的id指定注入的是哪个子类或实现类对象
    private UserDao dao;


    /**
     * 第二种注入方式:
     * @Value("#{bean的id}")
     */
    //@Value("#{userDaoImpl02}")
    //private UserDao dao;

    @Value("柳岩")
    private String name;


    /**
     * 第三种注入方式 : 是jdk的注解不是spring
     *  @Resource(name="bean的id值")
     */
    @Resource(name="userDaoImpl")
    private UserDao dao;

猜你喜欢

转载自blog.csdn.net/qq_43639081/article/details/109111796