1. 程式人生 > 其它 >SSM2.2【Spring:Spring註解開發】

SSM2.2【Spring:Spring註解開發】

Spring原始註解

Spring是輕程式碼而重配置的框架,配置比較繁重,影響開發效率,所以註解開發是一種趨勢,註解代替xml配置檔案可以簡化配置,提高開發效率。 
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 10 <bean id="dataSource_c3p0" class
="com.mchange.v2.c3p0.ComboPooledDataSource"> 11 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 12 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> 13 <property name="user" value="root"></property
> 14 <property name="password" value="root"></property> 15 </bean> 16 17 <!--載入外部的properties檔案--> 18 <!--需要在頂部beans中配置名稱空間context,並在xsi中新增對應的約束路徑--> 19 <context:property-placeholder location="classpath:jdbc.properties"/> 20 <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource"> 21 <property name="driverClassName" value="${jdbc.driver}"></property> 22 <property name="url" value="${jdbc.url}"></property> 23 <property name="username" value="${jdbc.username}"></property> 24 <property name="password" value="${jdbc.password}"></property> 25 </bean> 26 27 28 <!--<bean id="userDao" class="com.haifei.dao.impl.UserDaoImpl"></bean>--> 29 <!--<bean id="userService" class="com.haifei.service.impl.UserServiceImpl">--> 30 <!--<property name="userDao" ref="userDao"></property>--> 31 <!--</bean>--> 32 33 <!--配置元件掃描--> 34 <!--使用註解進行開發時(代替配置bean方式開發),需要在applicationContext.xml中配置元件掃描, 35 作用是指定哪個包及其子包下的Bean需要進行掃描以便識別使用註解配置的類、欄位和方法 36 不配置這個的話,執行會報異常NoSuchBeanDefinitionException--> 37 <context:component-scan base-package="com.haifei" /> 38 39 </beans>
1 package com.haifei.dao;
2 
3 public interface UserDao {
4 
5     public void save();
6 }
 1 package com.haifei.dao.impl;
 2 
 3 import com.haifei.dao.UserDao;
 4 import org.springframework.stereotype.Component;
 5 import org.springframework.stereotype.Repository;
 6 
 7 //<bean id="userDao" class="com.haifei.dao.impl.UserDaoImpl"></bean>
 8 //@Component("userDao")
 9 @Repository("userDao")
10 public class UserDaoImpl implements UserDao {
11 
12     @Override
13     public void save() {
14         System.out.println("UserDaoImpl: save running...");
15     }
16 }
1 package com.haifei.service;
2 
3 public interface UserService {
4 
5     public void save();
6 }
 1 package com.haifei.service.impl;
 2 
 3 import com.haifei.dao.UserDao;
 4 import com.haifei.dao.impl.UserDaoImpl;
 5 import com.haifei.service.UserService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.beans.factory.annotation.Qualifier;
 8 import org.springframework.beans.factory.annotation.Value;
 9 import org.springframework.context.annotation.Scope;
10 import org.springframework.stereotype.Component;
11 import org.springframework.stereotype.Service;
12 
13 import javax.annotation.PostConstruct;
14 import javax.annotation.PreDestroy;
15 import javax.annotation.Resource;
16 
17 //<bean id="userService" class="com.haifei.service.impl.UserServiceImpl"></bean>
18 //@Component("userService")
19 @Service("userService")
20 @Scope("prototype") //預設(不設定時也是)是singleton單例;可以設定為prototype多例
21 public class UserServiceImpl implements UserService {
22 
23     //<property name="userDao" ref="userDao"></property>
24     /*@Autowired //按照資料型別從Spring容器中進行匹配的(只寫Autowired,不寫Qualifier,也能注入)
25 //    @Qualifier("userDao") //是按照id值從容器中進行匹配的 但是主要此處@Qualifier結合@Autowired一起使用*/
26     @Resource(name="userDao") //@Resource相當於@Qualifier+@Autowired
27     private UserDao userDao;
28 
29     /*public void setUserDao(UserDao userDao) {
30         this.userDao = userDao;
31     }*/
32     /*
33     通過xml配置bean的方式開發時:
34         如果使用set進行依賴注入,則set方法必須有,不能省略
35     通過註解的方式開發時:
36         set方法可以省略,因為註解可以通過反射給userDao賦值
37      */
38 
39 
40     //通過註解注入普通資料型別
41     @Value("itcast")
42     private String heima; // private String heima = "itcast"
43     @Value("${jdbc.driver}") //通過鍵從jdbc.properties中獲取值
44     private String driver;
45 
46 
47     @Override
48     public void save() {
49         System.out.println(heima); //itcast
50         System.out.println(driver); //com.mysql.jdbc.Driver
51         userDao.save();
52     }
53 
54 
55     @PostConstruct
56     public void init(){
57         System.out.println("UserServiceImpl: 初始化方法");
58     }
59     @PreDestroy
60     public void destory(){
61         System.out.println("UserServiceImpl: 銷燬方法");
62     }
63 }
 1 package com.haifei.web;
 2 
 3 import com.haifei.service.UserService;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.springframework.stereotype.Controller;
 7 
 8 /**
 9  * 假的servlet
10  * 用於測試
11  */
12 //@Controller
13 public class UserController {
14 
15     public static void main(String[] args) {
16 //        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
17         ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
18         UserService userService = app.getBean(UserService.class);
19         userService.save();
20         app.close();
21 
22         /*
23             UserServiceImpl: 初始化方法
24             itcast
25             com.mysql.jdbc.Driver
26             UserDaoImpl: save running...
27             七月 15, 2021 7:37:53 下午 org.springframework.context.support.AbstractApplicationContext doClose
28             資訊: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e2144e4: startup date [Thu Jul 15 19:37:52 CST 2021]; root of context hierarchy
29          */
30     }
31 
32 }

Spring新註解