1. 程式人生 > >spring裝配bean(基於註解)

spring裝配bean(基於註解)

l  註解:就是一個類,使用@註解名稱

l  開發中:使用註解 取代 xml配置檔案。

l  註解使用前提,新增名稱空間,讓spring掃描含有註解類


1. @Component

@Component取代<bean class="">

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

UserService:

package com.itheima.g_annotation.a_ioc;

public interface UserService {
	
	public void addUser();

}


UserServiceImpl:

package com.itheima.g_annotation.a_ioc;

import org.springframework.stereotype.Component;

@Component("userServiceId")
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println("g_annotation.a_ioc add 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">
	<!-- 元件掃描,掃描含有註解的類 -->
	<context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan>
</beans>


TestAnnoIoC:

package com.itheima.g_annotation.a_ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnoIoC {
	
	@Test
	public void demo02(){
		//從spring容器獲得
		String xmlPath = "com/itheima/g_annotation/a_ioc/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		UserService userService = (UserService) applicationContext.getBean("userServiceId");
		userService.addUser();
		
	}

}


2.web開發

提供3個@Component註解衍生註解(功能一樣)取代<beanclass="">

       @Repository:dao層

       @Service:service層

       @Controller:web層

3.依賴注入    

給私有欄位設定,也可以給setter方法設定

       普通值:@Value("")

       引用值:

              方式1:按照【型別】注入

                     @Autowired

              方式2:按照【名稱】注入1

                     @Autowired

                     @Qualifier("名稱")

              方式3:按照【名稱】注入2

                     @Resource("名稱")


StudentDao:

package com.itheima.g_annotation.b_web;

public interface StudentDao {

	void save();

}

StudentDaoImpl:

package com.itheima.g_annotation.b_web;

import org.springframework.stereotype.Repository;

@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao {

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

}


StudentService:

package com.itheima.g_annotation.b_web;

public interface StudentService {

	void addStudent();

}


StudentServiceImpl:

package com.itheima.g_annotation.b_web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl implements StudentService {
	
	private StudentDao studentDao;
	
	@Autowired
	@Qualifier("studentDaoId")
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void addStudent() {
		studentDao.save();
	}

}


StudentAction:

package com.itheima.g_annotation.b_web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("studentActionId")
public class StudentAction {
	
	@Autowired //預設按照型別
	private StudentService studentService;

	public void execute() {
		studentService.addStudent();
	}

}


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">
	<!-- 元件掃描,掃描含有註解的類 -->
	<context:component-scan base-package="com.itheima.g_annotation.b_web"></context:component-scan>
</beans>


TestAnnoWeb:

package com.itheima.g_annotation.b_web;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnoWeb {
	
	@Test
	public void demo02(){
		//從spring容器獲得
		String xmlPath = "com/itheima/g_annotation/b_web/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		StudentAction studentAction = (StudentAction) applicationContext.getBean("studentActionId");
		
		studentAction.execute();
		
	}

}


4.生命週期

       初始化:@PostConstruct

       銷燬:@PreDestroy

5.作用域

       @Scope("prototype")多例

UserService:

package com.itheima.g_annotation.c_other;

public interface UserService {
	
	public void addUser();

}


UserServiceImpl:

package com.itheima.g_annotation.c_other;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service("userServiceId")
//@Scope("prototype")
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println("d_scope add user");
	}
	
	@PostConstruct
	public void myInit(){
		System.out.println("初始化");
	}
	@PreDestroy
	public void myDestroy(){
		System.out.println("銷燬");
	}

}


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">
	<!-- 元件掃描,掃描含有註解的類 -->
	<context:component-scan base-package="com.itheima.g_annotation.c_other"></context:component-scan>
</beans>


TestOther:

package com.itheima.g_annotation.c_other;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestOther {
	
	@Test
	public void demo02(){
		//spring 工廠
		String xmlPath = "com/itheima/g_annotation/c_other/beans.xml";
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		UserService userService = applicationContext.getBean("userServiceId" ,UserService.class);
		UserService userService2 = applicationContext.getBean("userServiceId" ,UserService.class);
		
		System.out.println(userService);
		System.out.println(userService2);
		
		applicationContext.close();
	}

}