1. 程式人生 > >Spring4.x + CXF3.x的JavaConfig配置類的純註解開發

Spring4.x + CXF3.x的JavaConfig配置類的純註解開發

最近在讀Spring in Action4這本書,受其啟發結合自己的業務,編寫這個JavaConfig配置類+CXF的純註解開發反例:

首先,準備環境:Spring4.x + cxf3.x + servlet3.x +Tomcat7,以上框架都要使用以上版本

其次,編寫用於替代Web.xml的類AbstractAnnotationConfigDispatcherServletInitializer,

Spring3.2引入了一個便利的WebApplicationInitializer的基礎實現,也就是AbstractAnnotationConfigDispatcherServletInitializer,配置該類可以用於啟動DespatcherServlet建立的SpringMVC上下文和ContextLoaderListener建立的Spring應用上下文。

例:

package com.nari.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.apache.cxf.transport.servlet.CXFServlet;
import org.apache.log4j.Logger;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.nari.coherence.listener.TaskServlet;

/**
 * 該類用於替換web.xml配置檔案:用於啟動web容器
 * @ClassName: StratApplicationContextInitializer
 * @Description:
 * @author: Fjw
 * @date: 2018年1月17日 上午8:57:40
 */
public class StratApplicationContextInitializer
  extends AbstractAnnotationConfigDispatcherServletInitializer{
 
 private static final Logger logger = Logger.getLogger(StratApplicationContextInitializer.class);

 /**
  * 返回帶有@Configuration註解的類,將會用來配置ContextLoaderListener建立的應用上下文中的bean
  * ApplicationContext(裡面包含了由該上下文建立的所有bean)
  */
 @Override
 protected Class<?>[] getRootConfigClasses() {
  // 建立一個配置類,用於registerContextLoaderListener底層方法的載入使用
  logger.info(">>>>>>root配置類初始化<<<<<<<<<");
  return new Class<?>[]{RootConfig.class};
 }

 /**
  * 返回帶有@Configuration註解的類
  * 用來返回由DispatcherServlet建立的應用上下文
  * DispatcherServlet(裡面包含了由該上下文建立的所有bean)
  *
  * 本專案未用到DispatcherServlet
  */
 @Override
 protected Class<?>[] getServletConfigClasses() {
  logger.info(">>>>>>web配置類初始化<<<<<<<<<");
  return null;
 }

 /**
  * 一個或多個路徑對映到DispatcherServlet上
  * 本專案未用到
  */
 @Override
 protected String[] getServletMappings() {
  logger.info(">>>>>>對映根路徑初始化<<<<<<<<<");
  return new String[]{ "/" };//請求路徑對映,根路徑

 }
 
 
 @Override
 public void onStartup(ServletContext servletContext) throws ServletException {
  
  // 在Web容器中新增一個用於初始化前置快取的Servlet
  Dynamic frontServlet = servletContext.addServlet("InitListener", TaskServlet.class);
  frontServlet.setLoadOnStartup(2);
  
  // 在Web容器中新增一個用於啟動CXF的Servlet
  Dynamic cxfServlet = servletContext.addServlet("cxf", CXFServlet.class);
  logger.info(">>>>>>新增CXF的Servlet<<<<<<<<<");
  cxfServlet.addMapping("/ws/*"); // 對映Servlet的路徑
  cxfServlet.setLoadOnStartup(1);
  
  // 在Web容器中新增一個Filter
  javax.servlet.FilterRegistration.Dynamic encodFilter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
  logger.info(">>>>>>新增一個編碼集的過濾器<<<<<<<<<");
  encodFilter.addMappingForServletNames(null, false, "/*");
  encodFilter.setInitParameter("encoding", "UTF-8");
  encodFilter.setInitParameter("forceEncoding", "true");
  
  registerContextLoaderListener(servletContext);
 }
}

第三:建立JavaConfig配置類 之 先編寫RootConfig配置類

package com.nari.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.nari.webservice.IEIGWSForPower;
import com.nari.webservice.impl.IEIGWSForPowerImpl;

/**
 * 用來返回由ContextLoaderListener建立的應用上下文 ApplicationContext(裡面包含了由該上下文建立的所有bean)
 * @ClassName: RootConfig
 * @Description:
 * @author: Fjw
 * @date: 2018年1月17日 上午9:17:27
 */

@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy // 該註解用於啟用Spring的自動代理功能
@Import({CXFConfig.class,DbcpDataSourceConfig.class,ServiceConfig.class}) // 該註解用於將其他配置類整合到本配置類中
@ComponentScan(basePackages=("com.nari")) // 啟動註解包掃描元件,掃描該包及其子包
public class RootConfig {
 
 /**
  * 以下三個是SPring上下文啟動時,設定的三個初始化引數(在base.properties裡面設定),
  */
 @Value("${corePoolSize}")// 該註解用於讀取當前上文中的屬性值
 private String corePoolSize;
 
 @Value("${maximumPoolSize}")
 private String maximumPoolSize;
 
 @Value("${keepAliveTime}")
 private String keepAliveTime;
 

// 建立提供WenService的服務類
 @Bean(name = "iEIGWSForPower")
 public IEIGWSForPower getIEIGWSForPower(){
  return new IEIGWSForPowerImpl();
 }

}

附:

其次,準備base.properties, log4j.properties連個properties檔案,

base.properties:

#database connection
dataSource.connection.url.mid=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST =  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = xe) ) )
dataSource.connection.driver_class.mid=oracle.jdbc.driver.OracleDriver
dataSource.connection.username.mid=scott
dataSource.connection.password.mid=tiger
#dataSource.default_schema.mid=fzptsea

#資料庫連線池
datasource.initialSize=5
datasource.minIdle=5
datasource.maxActive=50

#連線超時設定
datasource.maxWait=60000
#check leisure connection and close
datasource.timeBetweenEvictionRunsMillis=60000

第四:建立JavaConfig配置類 之 先編寫ServiceConfig配置類

package com.nari.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.nari.service.cj.HebeiCommandCJService;
import com.nari.service.cj.impl.HebeiCommandCJServiceImpl;
import com.nari.service.cj.impl.MeasureService;
import com.nari.service.mid.HebeiCommandMidService;
import com.nari.service.mid.impl.HebeiCommandMidServiceImpl;
import com.nari.service.quartz.HebeiQuartzService;
import com.nari.service.quartz.impl.HebeiQuartzServiceImpl;
import com.nari.util.UploadUtils;

/**
 *
 * @ClassName: ServiceConfig
 * @Description: 服務類的配置類
 * @author: Fjw
 * @date: 2018年1月17日 下午4:44:19
 */
@Configuration
public class ServiceConfig {
 
 @Bean(name = "hebeiCommandCJService")
 public HebeiCommandCJService getHebeiCommandCJService(){
  return new HebeiCommandCJServiceImpl();
 }
 
 @Bean(name = "hebeiCommandMidService")
 public HebeiCommandMidService getHebeiCommandMidService(){
  return new HebeiCommandMidServiceImpl();
 }
 
 @Bean(name = "hebeiQuartzService")
 public HebeiQuartzService getHebeiQuartzService(){
  return new HebeiQuartzServiceImpl();
 }
 
 @Bean(name = "measureService")
 public HebeiCommandCJService getMeasureService(){
  return new MeasureService();
 }
 
 @Bean(name = "uploadUtils")
 public UploadUtils getUploadUtils(){
  return new UploadUtils();
 }
}
依次為例配置Data Access Object 的配置類。

第五,建立cxf的JavaConfig 配置類

package com.nari.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import com.nari.service.cj.HebeiCommandCJService;
import com.nari.webservice.IEIGWSForPower;

/**
 * @ClassName: RootConfig
 * @Description: CXF的配置類
 * @author: Fjw
 * @date: 2018年1月17日 上午9:17:27
 */

@Configuration
@ImportResource({"classpath:META-INF/cxf/cxf.xml"})// 該註解用於將位於類路徑下的.xml配置檔案匯入JavaConfig類
public class CXFConfig {
 
 @Autowired
 private IEIGWSForPower iEIGWSForPower;
 
 /**
  * 用於攔截所有的訪問iEIGWSForPower服務的請求
  */
 @Autowired
 private Bus bus;
 
 @Bean(name = "serviceEndpoint")
 public Endpoint getServiceEndpoint(){
  
  // 引數1:請求攔截器(cxf自帶);引數2:提供服務的介面(注入時注入的是實現類)
  EndpointImpl endpoint = new EndpointImpl(bus, iEIGWSForPower);
  LOGGER.info(">>>>>>>>>>CXF服務釋出了<<<<<<<<<<<");
        endpoint.publish("/iEIGWSForPowerWS");
       
        System.out.println(hebeiCommandCJService.toString());
        return endpoint;
         }

}