Day21SpringDI-注釈

最後の研究の後、私たちは小さなケースを作ることができます

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
    }

このような構成コードは多数ありますが、より簡潔な方法は、注釈構成を使用することです。

春の依存関係の注入-オブジェクトを作成するための注釈***

  • (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)4つのメモ
//@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のままです。
次に、注釈によって注入が実現されます。

春の依存関係の注入-注釈の実装の注入***

  • (1)注射とは?
    検索後、割り当てを実行します
  • (2)3つの注入方法
  1. @Autowired
    または
    @
    Autowired @ Qualifier( "bean id")
  2. @Value( "#{bean的id}")
  3. @Resource(name = "bean id value")

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