1. 程式人生 > >Spring Bean配置方式之三:註解配置

Spring Bean配置方式之三:註解配置

Spring提供通過掃描類路徑中的特殊註解類來自動註冊Bean定義。同註解驅動事務一樣需要開啟自動掃描並註冊Bean定義支援,使用方式如下(resources/chapter12/ componentDefinitionWithAnnotation.xml):

java程式碼:
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:aop="http://www.springframework.org/schema/aop"
  3.     xmlns:context="http://www.springframework.org/schema/context"
  4.     xsi:schemaLocation="   
  5.        http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7.        http://www.springframework.org/schema/aop
  8.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  9.        http://www.springframework.org/schema/context
  10.        http:
    //www.springframework.org/schema/context/spring-context-3.0.xsd">
  11.     <aop:aspectj-autoproxy />   
  12.     <context:component-scan base-package="cn.javass.spring.chapter12"/>   
  13. </beans>  
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <aop:aspectj-autoproxy />

    <context:component-scan base-package="cn.javass.spring.chapter12"/>

</beans>

       使用<context:component-scan>標籤來表示需要要自動註冊Bean定義,而通過base-package屬性指定掃描的類路徑位置。

       <context:component-scan>標籤將自動開啟“註解實現Bean依賴注入”支援。

       此處我們還通過<aop:aspectj-autoproxy/>用於開啟Spring對@AspectJ風格切面的支援。

Spring基於註解實現Bean定義支援如下三種註解:

  • Spring自帶的@Component註解及擴充套件@Repository、@Service、@Controller,如圖12-1所示;
  • JSR-250 1.1版本中中定義的@ManagedBean註解,是Java EE 6標準規範之一,不包括在JDK中,需要在應用伺服器環境使用(如Jboss),如圖12-2所示;
  • JSR-330的@Named註解,如圖12-3所示。

 

圖12-1 Spring自帶的@Component註解及擴充套件

 

圖12-2 JSR-250中定義的@ManagedBean註解及自定義擴充套件

 

圖12-3 JSR-330的@Named註解及自定義擴充套件

圖12-2和圖12-3中的自定義擴充套件部分是為了配合Spring自帶的模式註解擴充套件自定義的,並不包含在Java EE 6規範中,在Java EE 6中相應的服務層、DAO層功能由EJB來完成。

在Java EE中有些註解執行放置在多個地方,如@Named允許放置在型別、欄位、方法引數上等,因此一般情況下放置在型別上表示定義,放置在引數、方法等上邊一般代表使用(如依賴注入等等)。

12.3.2  Spring自帶的@Component註解及擴充套件

一、@Component:定義Spring管理Bean,使用方式如下:

java程式碼:
@Component("識別符號")
POJO類

  在類上使用@Component註解,表示該類定義為Spring管理Bean,使用預設value(可選)屬性表示Bean識別符號。

1、定義測試Bean類:

java程式碼:
  1. import org.springframework.beans.factory.annotation.Autowired;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.stereotype.Component;   
  4. @Component("component")   
  5. publicclass TestCompoment {   
  6. @Autowired
  7. private ApplicationContext ctx;   
  8. public ApplicationContext getCtx() {   
  9. return ctx;   
  10.     }   
  11. }  
package cn.javass.spring.chapter12;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component("component")
public class TestCompoment {
    @Autowired
    private ApplicationContext ctx;
    public ApplicationContext getCtx() {
        return ctx;
    }
}

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試類和測試方法:

java程式碼:
  1. //省略import
  2. publicclass ComponentDefinitionWithAnnotationTest {   
  3. privatestatic String configLocation = "classpath:chapter12/componentDefinitionWithAnnotation.xml";   
  4. privatestatic ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);   
  5. @Test
  6. publicvoid testComponent() {   
  7.         TestCompoment component = ctx.getBean("component", TestCompoment.class);   
  8.         Assert.assertNotNull(component.getCtx());   
  9.     }   
  10. }  
package cn.javass.spring.chapter12;
//省略import
public class ComponentDefinitionWithAnnotationTest {
    private static String configLocation = "classpath:chapter12/componentDefinitionWithAnnotation.xml";
    private static ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
    @Test
    public void testComponent() {
        TestCompoment component = ctx.getBean("component", TestCompoment.class);
        Assert.assertNotNull(component.getCtx());
    }
}

    測試成功說明被@Component註解的POJO類將自動被Spring識別並註冊到Spring容器中,且自動支援自動裝配。

@AspectJ風格的切面可以通過@Compenent註解標識其為Spring管理Bean,而@Aspect註解不能被Spring自動識別並註冊為Bean,必須通過@Component註解來完成,示例如下:

java程式碼:
  1. //省略import
  2. @Component
  3. @Aspect
  4. publicclass TestAspect {   
  5. @Pointcut(value="execution(* *(..))")   
  6. privatevoid pointcut() {}   
  7. @Before(value="pointcut()")   
  8. publicvoid before() {   
  9.         System.out.println("=======before");   
  10.     }   
  11. }  
package cn.javass.spring.chapter12.aop;
//省略import
@Component
@Aspect
public class TestAspect {
    @Pointcut(value="execution(* *(..))")
    private void pointcut() {}
    @Before(value="pointcut()")
    public void before() {
        System.out.println("=======before");
    }
}

通過@Component將切面定義為Spring管理Bean。

二、@Repository:@Component擴充套件,被@Repository註解的POJO類表示DAO層實現,從而見到該註解就想到DAO層實現,使用方式和@Component相同;

1、定義測試Bean類:

java程式碼:
  1. import org.springframework.stereotype.Repository;   
  2. @Repository("testHibernateDao")   
  3. publicclass TestHibernateDaoImpl {   
  4. }  
package cn.javass.spring.chapter12.dao.hibernate;
import org.springframework.stereotype.Repository;
@Repository("testHibernateDao")
public class TestHibernateDaoImpl {
 
}

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testDao() {   
  2. TestHibernateDaoImpl dao =   
  3. ctx.getBean("testHibernateDao", TestHibernateDaoImpl.class);   
  4. Assert.assertNotNull(dao);   
  5. }  
@Test
public void testDao() {
TestHibernateDaoImpl dao =
ctx.getBean("testHibernateDao", TestHibernateDaoImpl.class);
Assert.assertNotNull(dao);
}

    測試成功說明被@Repository註解的POJO類將自動被Spring識別並註冊到Spring容器中,且自動支援自動裝配,並且被@Repository註解的類表示DAO層實現。

三、@Service:@Component擴充套件,被@Service註解的POJO類表示Service層實現,從而見到該註解就想到Service層實現,使用方式和@Component相同;

1、定義測試Bean類:

java程式碼:
  1. import org.springframework.beans.factory.annotation.Autowired;   
  2. import org.springframework.beans.factory.annotation.Qualifier;   
  3. import org.springframework.stereotype.Service;   
  4. import cn.javass.spring.chapter12.dao.hibernate.TestHibernateDaoImpl;   
  5. @Service("testService")   
  6. publicclass TestServiceImpl {   
  7. @Autowired
  8. @Qualifier("testHibernateDao")   
  9. private TestHibernateDaoImpl dao;   
  10. public TestHibernateDaoImpl getDao() {   
  11. return dao;   
  12.     }   
  13. }  
package cn.javass.spring.chapter12.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.javass.spring.chapter12.dao.hibernate.TestHibernateDaoImpl;
@Service("testService")
public class TestServiceImpl {
    @Autowired
    @Qualifier("testHibernateDao")
    private TestHibernateDaoImpl dao;
    public TestHibernateDaoImpl getDao() {
        return dao;
    }
}

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testService() {   
  2.     TestServiceImpl service = ctx.getBean("testService", TestServiceImpl.class);   
  3.     Assert.assertNotNull(service.getDao());   
  4. }  
@Test
public void testService() {
    TestServiceImpl service = ctx.getBean("testService", TestServiceImpl.class);
    Assert.assertNotNull(service.getDao());
}

測試成功說明被@Service註解的POJO類將自動被Spring識別並註冊到Spring容器中,且自動支援自動裝配,並且被@Service註解的類表示Service層實現。

四、@Controller:@Component擴充套件,被@Controller註解的類表示Web層實現,從而見到該註解就想到Web層實現,使用方式和@Component相同;

1、定義測試Bean類:

java程式碼:
  1. //省略import
  2. @Controller
  3. publicclass TestAction {   
  4. @Autowired
  5. private TestServiceImpl testService;   
  6. publicvoid list() {   
  7. //呼叫業務邏輯層方法
  8.     }   
  9. }   
package cn.javass.spring.chapter12.action;
//省略import
@Controller
public class TestAction {
    @Autowired
    private TestServiceImpl testService;
   
    public void list() {
        //呼叫業務邏輯層方法
    }
}
 
 
 

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testWeb() {   
  2.     TestAction action = ctx.getBean("testAction", TestAction.class);   
  3.     Assert.assertNotNull(action);   
  4. }  
@Test
public void testWeb() {
    TestAction action = ctx.getBean("testAction", TestAction.class);
    Assert.assertNotNull(action);
}

    測試成功說明被@Controller註解的類將自動被Spring識別並註冊到Spring容器中,且自動支援自動裝配,並且被@Controller註解的類表示Web層實現。

大家是否注意到@Controller中並沒有定義Bean的識別符號,那麼預設Bean的名字將是以小寫開頭的類名(不包括包名),即如“TestAction”類的Bean識別符號為“testAction”。

六、自定義擴充套件:Spring內建了三種通用的擴充套件註解@Repository、@Service、@Controller ,大多數情況下沒必要定義自己的擴充套件,在此我們演示下如何擴充套件@Component註解來滿足某些特殊規約的需要;

在此我們可能需要一個快取層用於定義快取Bean,因此我們需要自定義一個@Cache的註解來表示快取類。

1、擴充套件@Component:

java程式碼:
  1. //省略import
  2. @Target({ElementType.TYPE})   
  3. @Retention(RetentionPolicy.RUNTIME)   
  4. @Documented
  5. @Component
  6. public@interface Cache{   
  7.        String value() default"";   
  8. }   
package cn.javass.spring.chapter12.stereotype;
//省略import
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Cache{
       String value() default "";
}
 

    擴充套件十分簡單,只需要在擴充套件的註解上註解@Component即可,@Repository、@Service、@Controller也是通過該方式實現的,沒什麼特別之處

2、定義測試Bean類:

java程式碼:
package cn.javass.spring.chapter12.cache;
@Cache("cache")
public class TestCache {
 
}

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testCache() {   
  2.     TestCache cache = ctx.getBean("cache", TestCache.class);   
  3.     Assert.assertNotNull(cache);   
  4. }  
@Test
public void testCache() {
    TestCache cache = ctx.getBean("cache", TestCache.class);
    Assert.assertNotNull(cache);
}

    測試成功說明自定義的@Cache註解也能很好的工作,而且實現了我們的目的,使用@Cache來表示被註解的類是Cache層Bean。

12.3.3  JSR-250中定義的@ManagedBean註解

@javax.annotation.ManagedBean需要在實現Java EE 6規範的應用伺服器上使用,雖然Spring3實現了,但@javax.annotation.ManagedBean只有在Java EE 6環境中才有定義,因此測試前需要我們定義ManagedBean類。

1、定義javax.annotation.ManagedBean註解類:

java程式碼:
  1. import java.lang.annotation.ElementType;   
  2. import java.lang.annotation.Retention;   
  3. import java.lang.annotation.RetentionPolicy;   
  4. import java.lang.annotation.Target;   
  5. @Target(ElementType.TYPE)   
  6. @Retention(RetentionPolicy.RUNTIME)   
  7. public@interface ManagedBean {   
  8.      String value() default"";   
  9. }  
package javax.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ManagedBean {
     String value() default "";
}

其和@Component完全相同,唯一不同的就是名字和建立者(一個是Spring,一個是Java EE規範)。

2、定義測試Bean類:

java程式碼:
  1. import javax.annotation.Resource;   
  2. import org.springframework.context.ApplicationContext;   
  3. @javax.annotation.ManagedBean("managedBean")   
  4. publicclass TestManagedBean {   
  5. @Resource
  6. private ApplicationContext ctx;   
  7. public ApplicationContext getCtx() {   
  8. return ctx;   
  9.     }   
  10. }  
package cn.javass.spring.chapter12;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
@javax.annotation.ManagedBean("managedBean")
public class TestManagedBean {
    @Resource
    private ApplicationContext ctx;
    public ApplicationContext getCtx() {
        return ctx;
    }
}

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testManagedBean() {   
  2.     TestManagedBean testManagedBean = ctx.getBean("managedBean", TestManagedBean.class);   
  3.     Assert.assertNotNull(testManagedBean.getCtx());   
  4. }  
@Test
public void testManagedBean() {
    TestManagedBean testManagedBean = ctx.getBean("managedBean", TestManagedBean.class);
    Assert.assertNotNull(testManagedBean.getCtx());
}

    測試成功說明被@ManagedBean註解類也能正常工作。

自定義擴充套件就不介紹了,大家可以參考@Component來完成如圖12-2所示的自定義擴充套件部分。

12.3.4  JSR-330的@Named註解

@Named不僅可以用於依賴注入來指定注入的Bean的識別符號,還可以用於定義Bean。即註解在型別上表示定義Bean,註解在非型別上(如欄位)表示指定依賴注入的Bean識別符號。

1、定義測試Bean類:

java程式碼:
  1. //省略import
  2. @Named("namedBean")   
  3. publicclass TestNamedBean {   
  4. @Inject
  5. private ApplicationContext ctx;   
  6. public ApplicationContext getCtx() {   
  7. return ctx;   
  8.     }   
  9. }   
package cn.javass.spring.chapter12;
//省略import
@Named("namedBean")
public class TestNamedBean {
    @Inject
    private ApplicationContext ctx;
    public ApplicationContext getCtx() {
        return ctx;
    }
}
 

2、Spring配置檔案使用chapter12/ componentDefinitionWithAnnotation.xml即可且無需修改;

3、定義測試方法:

java程式碼: Java程式碼 @Test
  1. publicvoid testNamedBean() {   
  2. TestNamedBean testNamedBean =   
  3.     ctx.getBean("namedBean", TestNamedBean.class);   
  4.     Assert.assertNotNull(testNamedBean.getCtx());   
  5. }  
@Test
public void testNamedBean() {
TestNamedBean testNamedBean =
    ctx.getBean("namedBean", TestNamedBean.class);
    Assert.assertNotNull(testNamedBean.getCtx());
}

測試成功說明被@Named註解類也能正常工作。

自定義擴充套件就不介紹了,大家可以參考@Component來完成如圖12-3所示的自定義擴充套件部分。

12.3.5  細粒度控制Bean定義掃描

在XML配置中完全消除了Bean定義,而是隻有一個<context:component-scan>標籤來支援註解Bean定義掃描。

前邊的示例完全採用預設掃描設定,如果我們有幾個元件不想被掃描並自動註冊、我們想更改預設的Bean識別符號生成策略該如何做呢?接下來讓我們看一下如何細粒度的控制Bean定義掃描,具體定義如下:

java程式碼:
  1.         base-package=""
  2.         resource-pattern="**/*.class"
  3.         name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator"
  4.         use-default-filters="true"
  5.         annotation-config="true">   
  6.                 <context:include-filter type="aspectj" expression=""/>   
  7.                 <context:exclude-filter type="regex" expression=""/>   
  8. </context:component-scan>     
<context:component-scan
        base-package=""
        resource-pattern="**/*.class"
        name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator"
        use-default-filters="true"
        annotation-config="true">
                <context:include-filter type="aspectj" expression=""/>
                <context:exclude-filter type="regex" expression=""/>
</context:component-scan>   
  • base-package:表示掃描註解類的開始位置,即將在指定的包中掃描,其他包中的註解類將不被掃描,預設將掃描所有類路徑;
  • resource-pattern:表示掃描註解類的字尾匹配模式,即“base-package+resource-pattern”將組成匹配模式用於匹配類路徑中的元件,預設字尾為“**/*.class”,即指定包下的所有以.class結尾的類檔案;
  • name-generator:預設情況下的Bean識別符號生成策略,預設是AnnotationBeanNameGenerator,其將生成以小寫開頭的類名(不包括包名);可以自定義自己的識別符號生成策略;
  • use-default-filters:預設為true表示過濾@Component、@ManagedBean、@Named註解的類,如果改為false預設將不過濾這些預設的註解來定義Bean,即這些註解類不能被過濾到,即不能通過這些註解進行Bean定義;
  • annotation-config:表示是否自動支援註解實現Bean依賴注入,預設支援,如果設定為false,將關閉支援註解的依賴注入,需要通過<context:annotation-config/>開啟。

預設情況下將自動過濾@Component、@ManagedBean、@Named註解的類並將其註冊為Spring管理Bean,可以通過在<context:component-scan>標籤中指定自定義過濾器將過濾到匹配條件的類註冊為Spring管理Bean,具體定義方式如下:

java程式碼:
<context:include-filter type="aspectj" expression=""/>
<context:exclude-filter type="regex" expression=""/>
  • <context:include-filter>:表示過濾到的類將被註冊為Spring管理Bean;
  • <context:exclude-filter>:表示過濾到的類將不被註冊為Spring管理Bean,它比<context:include-filter>具有更高優先順序;
  • type:表示過濾器型別,目前支援註解型別、類型別、正則表示式、aspectj表示式過濾器,當然也可以自定義自己的過濾器,實現org.springframework.core.type.filter.TypeFilter即可;
  • expression:表示過濾器表示式。

一般情況下沒必要進行自定義過濾,如果需要請參考如下示例:

1、cn.javass.spring.chapter12.TestBean14自動註冊為Spring管理Bean:

2、把所有註解為org.aspectj.lang.annotation.Aspect自動註冊為Spring管理Bean:

java程式碼:
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>

3、將把匹配到正則表示式“cn\.javass\.spring\.chapter12\.TestBean2*”排除,不註冊為Spring管理Bean:

4、將把匹配到aspectj表示式“cn.javass.spring.chapter12.TestBean3*”排除,不註冊為Spring管理Bean:

具體使用就要看專案需要了,如果以上都不滿足需要請考慮使用自定義過濾器。

12.3.6  提供更多的配置元資料

1、@Lazy:定義Bean將延遲初始化,使用方式如下:

java程式碼:
  1. @Lazy(true)   
  2. publicclass TestCompoment {   
  3. ……   
  4. }  
@Component("component")
@Lazy(true)
public class TestCompoment {
……
}

    使用@Lazy註解指定Bean需要延遲初始化。

2、@DependsOn:定義Bean初始化及銷燬時的順序,使用方式如下:

java程式碼:
  1. @DependsOn({"managedBean"})   
  2. publicclass TestCompoment {   
  3. ……   
  4. }  
@Component("component")
@DependsOn({"managedBean"})
public class TestCompoment {
……
}

3、@Scope:定義Bean作用域,預設單例,使用方式如下:

java程式碼:
  1. @Scope("singleton")   
  2. publicclass TestCompoment {   
  3. ……   
  4. }  
@Component("component")
@Scope("singleton")
public class TestCompoment {
……
}

4、@Qualifier:指定限定描述符,對應於基於XML配置中的<qualifier>標籤,使用方式如下:

java程式碼: Java程式碼
  1. @Component("component")   
  2. @Qualifier("component")   
  3. publicclass TestCompoment {   
  4. ……   
  5. }  
@Component("component")
@Qualifier("component")
public class TestCompoment {
……
}

    可以使用複雜的擴充套件,如@Mysql等等。

5、@Primary:自動裝配時當出現多個Bean候選者時,被註解為@Primary的Bean將作為首選者,否則將丟擲異常,使用方式如下:

相關推薦

Spring Bean配置方式註解配置

Spring提供通過掃描類路徑中的特殊註解類來自動註冊Bean定義。同註解驅動事務一樣需要開啟自動掃描並註冊Bean定義支援,使用方式如下(resources/chapter12/ componentDefinitionWithAnnotation.xml): jav

Hadoop學習------Hadoop安裝方式()分布式部署

之間 root用戶 jar .sh author tables eth1 report 標識 這裏為了方便直接將單機部署過的虛擬機直接克隆,當然也可以不這樣做,一個個手工部署。 創建完整克隆——>下一步——>安裝位置。等待一段時間即可。 我這邊用了三臺虛擬

自定義spring boot starter三部曲原始碼分析spring.factories載入過程

本文是《自定義spring boot starter三部曲》系列的終篇,前文中我們開發了一個starter並做了驗證,發現關鍵點在於spring.factories的自動載入能力,讓應用只要依賴starter的jar包即可,今天我們來分析Spring和Spring boot原始碼,瞭解s

Spring原理學習系列Spring AOP原理(從原始碼層面分析)-------上部

引言 本文是Spring原理分析的第三篇博文,主要闡述Spring AOP相關概念,同時從原始碼層面分析AOP實現原理。對於AOP原理的理解有利於加深對Spring框架的深入理解。同時我也希望可以探究Spring框架在處理AOP的解決思路,學習框架的時候,有時候

spring事務管理宣告式事務管理使用xml配置檔案的方式

這種方式是開發中常見的一種方式:利用aop的思想,將需要事務管理的業務方法通過xml配置的方式,將事務管理加在該類的相關方法上。這種方法的優點是,一次xml配置,後期不用關心業務類增加或者減少,通過xml中配置的匹配資訊,會去找業務類所在的包和方法,然後加上事務。 重點是配置<tx:a

SpringBean配置方式FactoryBean

alt mage -1 bsp image 技術分享 logs 指向 urn   Spring 中有兩種類型的 Bean, 一種是普通Bean, 另一種是工廠Bean, 即FactoryBean.   工廠 Bean 跟普通Bean不同, 其返回的對象不是指定類的一個實例,

Spring CloudZuul()路由配置詳解

主題 路由配置詳解 前言 現實中可能只想讓Zuul代理部分微服務,又或者需要對URL進行更加精確的控制。Zuul的路由配置非常靈活、簡單,本部落格通過幾個例項,詳細講解Zuul的路由配置。 內容 1.自定義指定微服務的訪問路徑 說明:配置zuul.routes.指定微服務

spring-bean生命週期初始化和銷燬的方式

1,註解bean之指定init-method/destroy-method 這種方式spring註解之@Bean註解,這邊再簡單演示如下: 配置類中增加一個bean如下: /** * 定義一個bean物件 * @return */

Spring4學習(註解配置bean

一、註解基礎 1、元件掃描(component scanning) Spring能夠從classpath下自動掃描,偵測和例項化具有特定註解的元件 2、特定元件包括 - @Component 基本註解,標示一個受spring管理的元件 - @Respository 標識持久

VCSA 6.5 HA配置 準備工作

vmware vcenter ha 高可用 vcsa 接著上一篇文章部署完成VCSA 6.5後,還需要做一些準備工作才能開啟高可用功能,本篇文章主要就講述如何為vCenter 高可用進行準備工作配置vCenter HA網絡從vCenter HA的架構圖中可以看出對於vCenter HA的高

SpringAOP的註解配置

函數 cts expr pro text bsp 定義 一個 rod 配置過程可以簡單的分為3步: 1,業務類配置 在業務類前加入,將業務類交由Spring管理 @Component("s") 這個表示,這個業務類的Bean名字為 s 。 2,將切點和切面

Https系列讓服務器同時支持http、https,基於spring boot

signed 默認 gfs proc idl clas 兩種方法 .... gpg Https系列會在下面幾篇文章中分別作介紹: 一:https的簡單介紹及SSL證書的生成二:https的SSL證書在服務器端的部署,基於tomcat,spring boot三:讓服務器同時

spring boot 系列spring boot 整合JdbcTemplate

closed com context boot pin pan url wired ace 前面兩篇文章我們講了兩件事情: 通過一個簡單實例進行spring boot 入門 修改spring boot 默認的服務端口號和默認context path 這篇文章我們來看下怎

spring-bean實例化方式

java imp cto -i 實例 cat .get view pan 在spring中,bean的示例化有三種方式。 1、使用類的無參構造函數創建 2、使用靜態工廠方式創建 3、使用實例化工廠方式創建。 具體代碼如下 靜態工廠方式: Bean2.java

如何安裝和配置打印服務器設置打印機打印優先級別

sha 基礎架構 打印 fin 優先級 jpg str 兩種 finish 如何安裝和配置打印服務器之三:設置打印機打印優先級別 ?Lander Zhang 專註外企按需IT基礎架構運維服務,IT Helpdesk 實戰培訓踐行者http://blog.51cto.com/

(12)Spring學習記錄---Spring_bean(Spring_通過註解配置 Bean

用註解標識特定的元件,用元件掃描讓系統自動找到特點的元件       例項: 1.建立4個包  (1)annotation       TestObject.java impo

Spring Boot 系統Spring Boot 整合JdbcTemplate

前面兩篇文章我們講了兩件事情: 通過一個簡單例項進行Spring Boot 入門 修改Spring Boot 預設的服務埠號和預設context path 這篇文章我們來看下怎麼通過JdbcTemplate進行資料的持久化。 一、程式碼實現 1、修改pom.xml檔案

SpringSecurity學習筆記配置使用者儲存

沒有使用者儲存的應用相當於沒有使用者,因為任何使用者都會被拒之門外。我們所需要的是使用者儲存,也就是使用者名稱、密碼以及其他資訊儲存的地方,在進行認證決策的時候,會對其進行檢索。Spring Security非常靈活,能夠基於各種資料儲存來認證使用者。它內建了多種常見的使用者

效能調優MySQL篇MySQL配置定位以及優化

1、優化方式 一般的優化方法有:硬體優化,配置優化,sql優化,表結構優化。下面僅僅介紹配置優化,具體優化設定可以參考本人另外一篇部落格,傳送門:https://www.cnblogs.com/langhuagungun/p/9507206.html 2、mysql配置分析 1)常見瓶頸 90%系統瓶

Spring中<bean>標籤使用p標籤配置bean的屬性

在spring的bean配置檔案中我們常可以見到下面的例子: <bean id="user" class="com.sys.User" p:name-ref="name" /> 其中,p:name-ref="name"使用了p標籤來配置bean的name的引用。在使用p標籤配