Spring筆記(6)—— @Component註解

1 註解注入

  • 註解:就是一個類,使用@註解名稱
  • 開發中:使用註解 取代 xml配置文件

1.1 @Component

  • @Component 取代 <bean class="">
  • @Component("id") 取代 <bean id="" class="">

1.2 web開發,提供3個@Component註解衍生註解(功能一樣)取代<bean class="">

  • @Repository(“名稱”):dao層

  • @Service(“名稱”):service層

  • @Controller(“名稱”):web層

  • @Autowired:自動根據類型注入

  • @Qualifier(“名稱”):指定自動注入的id名稱

  • @Resource(“名稱”)

  • @PostConstruct 自定義初始化

  • @PreDestroy 自定義銷燬

2 測試

2.1 xml 配置

  • IUserService
package com.tzb.service;
import com.tzb.model.User;
public interface IUserService {
    public void add();
    public void add(User user);
}

  • UserServiceImpl
package com.tzb.service;

import com.tzb.model.User;

public class UserServiceImpl implements IUserService {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void add() {
        System.out.println("添加用戶:" + name);
    }

    @Override
    public void add(User user) {
        System.out.println("添加用戶:"+ user);
    }
}

  • beans.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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id = "userService" class="com.tzb.service.UserServiceImpl"></bean>

</beans>
  • 測試
@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans10.xml");
        IUserService service = (IUserService) context.getBean("userService");
        User user = new User();
        user.setUsername("Mike");
        service.add(user);
    }

2.2 使用註解

  • 默認註解不生效,需要開啓

  • UserServiceImpl
package com.tzb.service;

import com.tzb.model.User;
import org.springframework.stereotype.Component;

@Component
public class UserServiceImpl implements IUserService {
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void add() {
        System.out.println("添加用戶:" + name);
    }

    @Override
    public void add(User user) {
        System.out.println("添加用戶:"+ user);
    }
}

  • beans.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">

    <!-- <bean id = "userService" class="com.tzb.service.UserServiceImpl"></bean>-->

 <!--開啓註解-->
   <context:annotation-config>
   </context:annotation-config>

 <!--註解的位置-->
  <context:component-scan base-package="com.tzb"></context:component-scan>

</beans>
  • 測試
   @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans10.xml");
        /*
        * 如果 @Component 沒有配置id,那就通過類型獲取
        * 類型:接口或者實現類
        * */
        IUserService service = context.getBean(UserServiceImpl.class);
        User user = new User();
        user.setUsername("Mike");
        service.add(user);
    }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章