Spring入門知識 ———— 通過註解的方式配置Bean
一、引言
小夥伴們注意啦,重點,重點來了
之前我們都是通過xml去配置一個bean,當然我們在實際專案中肯定不是那麼幹的,畢竟一個專案那麼多的類,挨個挨個配置豈不是太麻煩了。到目前為止最為流行的方式還是註解,只需簡單加上註解即可新增到IOC容器當中去,但是之前對於bean的操作還是得去了解。那麼今天小編就來講講如果使用註解的方式去載入bean,看好啦。
二、註解包括
Spring可以通過自動掃描,偵測那些類是有包含註解的,那麼註解分為這麼幾類:
@Component:基本註解,標識一個受Spring管理的類
@Respository:標識持久層的類
@Service:標識業務層(服務層)的類
@Controller:標識表現層(控制層)元件
相信大家對這些註解肯定不陌生,也相信大家都有使用過,其實這幾個註解的作用都是一樣的,只是為了表示規範一點,所以進行了分類。
三、例項操作
例項一:使用@Component來標識一個受Spring管理的類
/** * 汽車類 */ @Component public class Car { /** * 汽車品牌 */ private String brand; /** * 汽車產地 */ private String origin; /** * 汽車價格 */ public int price; }
例項二:使用@Respository來標識一個持久層,一個介面類和一個實現類
public interface CarDao {
/**
* 加添汽車
* @param car
*/
public void saveCar(Car car);
}
@Repository public class CarDaoImpl implements CarDao { public void saveCar(Car car) { System.out.println("CarDaoImpl.saveCar ....."); } }
例項三:使用@Service來標識業務層,同樣一個介面類和一個實現類
public interface CarService {
/**
* 加添汽車
* @param car
*/
public void saveCar(Car car);
}
@Service
public class CarServiceImpl implements CarService {
public void saveCar(Car car) {
System.out.println("CarServiceImpl saveCar .....");
}
}
例項四:最後使用@Controller註解來標識表現層
@Controller
public class CarController {
public void saveCar(){
System.out.println("CarController.saveCar .....");
}
}
最後重點,注意:既然我們為每一個類都寫上了註解,可是Spring IOC容器知道要去哪裡掃描這些類嗎?那當然是不知道的,所以我們還需要告訴Spring掃描那些包。
<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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--先要匯入context的名稱空間-->
<!--通知Spring IOC容器,指定掃描哪些包-->
<context:component-scan base-package="com.spring.six"></context:component-scan>
</beans>
測試測試測試:寫上註解之後,只需要配置IOC掃描的包,即可獲得Bean物件,是不是非常非常非常方便。
在這裡需要注意一點,getBean("")裡面每個首字母都是小寫,這裡涉及到Spring有預設的命名策略。
如果沒有指定獲取Bean的定類名,第一個首字母小寫即可,也可以在註解中通過value屬性指定獲取Bean的名字。
比如:@Controller(value = "carContor"),獲取bean是就需要getBean("carContor")
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-six.xml");
Car car = (Car) beanFactory.getBean("car");
System.out.println("car --->"+car);
CarDao carDao = (CarDao) beanFactory.getBean("carDaoImpl");
System.out.println("carDao --- >"+carDao);
carDao.saveCar(car);
CarService carService = (CarService) beanFactory.getBean("carServiceImpl");
System.out.println("carService --- >" + carService);
carService.saveCar(car);
CarController carController = (CarController) beanFactory.getBean("carController");
System.out.println("carController --- >" + carController);
carController.saveCar();
}
最後看結果:憑小編多年的經驗,那肯定是正常列印呀哈哈哈哈~~~
十一月 01, 2018 9:14:03 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org[email protected]246b179d: startup date [Thu Nov 01 21:14:03 CST 2018]; root of context hierarchy
十一月 01, 2018 9:14:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [applicationContext-six.xml]
car --->[email protected]
carDao --- >[email protected]
CarDaoImpl.saveCar .....
carService --- >[email protected]
CarServiceImpl.saveCar .....
carController --- >[email protected]
CarController.saveCar .....
Process finished with exit code 0
四、最後彩蛋環節
最後彩蛋如果不看,那則是人生一大悲哀呀~
當在每個類使用了特定的註解之後,還需要在Spring的配置檔案中宣告<context:component-scan>:
base-package屬性:指定一個需要掃描的包名,Spring容器則會自動掃描這個包中所有的子包,需要掃描多個包時,使用逗號隔開即可。
那麼有個問題,如果想其中某一個類,不想讓Srping容器掃描到怎麼辦?
<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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--先要匯入context的名稱空間-->
<!--通知Spring IOC容器,掃描哪些包-->
<context:component-scan base-package="com.spring.six">
<!--
context:include-filter :表示在com.spring.six包中,
type="assignable": 只會掃描com.spring.six.controller.CarController這一個類
type="annotation": 只會掃描帶有@Controller註解的類,其他類則不會掃描
-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
<!--
ontext:exclude-filter :則是和以上相反
-->
<!-- <context:exclude-filter type="assignable" expression="com.spring.six.controller.CarController"></context:exclude-filter>-->
</context:component-scan>
</beans>