spring_ioc相關_第二章

繼spring_ioc相關_第一章之後,繼續ioc的第二章內容

上章內容回顧:

1 spring的概念

(1)核心:ioc和aop

(2)spring一站式框架

2 spring的bean管理(xml)

(1)bean實例化

(2)注入屬性

(3)注入對象屬性

3 ioc和di

(1)ioc:控制反轉,把對象創建交給spring管理

(2)di:依賴注入,創建對象過程中,向屬性設置值

4 在服務器啓動時候加載配置文件,創建對象

(1)ServletContext對象

(2)監聽器

這章內容簡介:

  spring的bean管理(註解)

(1)使用註解創建對象

- 四個註解

(2)使用註解注入屬性

- Autowired

- Resource

(3)xml和註解方式混合使用

- 創建對象使用配置文件,注入屬性使用註解

 

 spring註解開發準備

(1) 導入基本的jar包

(2) 創建類 創建方法

(3) 創建spring配置文件,引入約束 schema約束有兩個: 1第一天的基本功能,引入約束beans 2ioc的註解開發,引入新的約束

<?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="cn.hr.*"></context:component-scan>
        
</beans>

(4) 開啓註解掃描

其中cn.hr.* 爲要掃描spring註解的所有包文件

 

  註解創建對象

創建對象有四個註解

@Component

@Controller  : 業務層

@Service : 業務層

@Respository: 持久層

有註釋的三個註解是爲了讓標註類本身的用途清晰,Spring會在後續版本對齊增強,目前這四個註解功能一致,都創建對象

創建對象是單實例還是多實例: spring默認是單實例的,線程不安全的,若要多實例則使用@Scope註解如:

 

註解注入屬性

1 創建service類,創建dao類,在service得到dao對象

注入屬性第一個註解 @Autowired

1)創建daoservice對象

2)在service類裏面定義dao類型屬性

 

注入屬性第二個註解 @Resource

 

實例代碼:

CarService中關聯CarDao,CarDao中引入Car

CarService:

package cn.hr.service;

import javax.annotation.Resource;

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

import cn.hr.dao.CarDao;

public class CarService {
	@Autowired
	private CarDao carDao;
	//service中引入dao
	public void add() {
		System.out.println("carService add...");
		carDao.add();
		
	}
}

CarDao:

package cn.hr.dao;

import javax.annotation.Resource;

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

import cn.hr.bean.Car;

@Service(value="carDao")
public class CarDao {
//	@Autowired
//	private Car car;  //使用註解注入對象屬性實現一
	
	@Resource(name="car")
	private Car car;   //使用註解注入對象屬性二
	
	public void add() {
		car.setCarName("bmw");
		car.setPrice(200000);
		System.out.println("cardao add...." + car.toString());
	}
}

Car:

package cn.hr.bean;

import org.springframework.stereotype.Component;

@Component(value="car")
public class Car {
	private String carName;
	private int price;

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}



	public void add() {
		System.out.println("增加一輛車");
	}

	@Override
	public String toString() {
		return "Car [carName=" + carName + ", price=" + price + "]";
	}
	
}

測試類:

package cn.hr.test;

import org.junit.Test;
import org.junit.validator.PublicClassValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.hr.bean.Car;
import cn.hr.service.CarService;

public class TestAnnotation {

	@Test
	public void car() {
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		Car car=  (Car) context.getBean("car");
		car.add();
	}
	
	@Test
	public void add() {
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		CarService carService = (CarService) context.getBean("carService");
		carService.add();
	}
	
	
}

github:https://github.com/2402zmybie/spring_ioc_02

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