文件結構html
1、interface、bean等代碼java
package com.yiibai.core; public interface IHelloWorld { public void printHello(); void printHelloWorld(String msg); }
package com.yiibai.core; public class HelloWorldImpl implements IHelloWorld { private String name; @Override public void printHello() { // TODO 自動生成的方法存根 System.out.println("Spring 3 : Hello ! " + name); } public void setName(String name) { this.name = name; } @Override public void printHelloWorld(String msg) { // TODO 自動生成的方法存根 System.out.println("Hello : " + msg); } }
package com.yiibai.core; public class CustomerBo { public void printMsg(String msg) { System.out.println("CustomerBo : " + msg); } }
package com.yiibai.core; public class SchedulerBo { public void printMsg(String msg) { System.out.println("SchedulerBo : " + msg); } }
2、AppConfig(至關於配置文件)web
package com.yiibai.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.yiibai.core.CustomerBo; @Configuration public class CustomerConfig { @Bean(name="customer") public CustomerBo customerBo(){ return new CustomerBo(); } }
package com.yiibai.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.yiibai.core.SchedulerBo; @Configuration public class SchedulerConfig { @Bean(name="scheduler") public SchedulerBo suchedulerBo(){ return new SchedulerBo(); } }
package com.yiibai.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import com.yiibai.core.HelloWorldImpl; import com.yiibai.core.IHelloWorld; @Configuration @Import({ CustomerConfig.class, SchedulerConfig.class }) public class AppConfig { @Bean(name="helloBean") public IHelloWorld helloWorld() { return new HelloWorldImpl(); } }
3、測試(主類)spring
package com.yiibai.config; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.yiibai.core.CustomerBo; import com.yiibai.core.IHelloWorld; import com.yiibai.core.SchedulerBo; public class App { private static ApplicationContext context; public static void main(String[] args) { context = new AnnotationConfigApplicationContext(AppConfig.class); IHelloWorld obj = (IHelloWorld) context.getBean("helloBean"); obj.printHelloWorld("Spring Java Config"); CustomerBo customer = (CustomerBo) context.getBean("customer"); customer.printMsg("Hello 11"); SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler"); scheduler.printMsg("Hello 22"); } }
4、運行編程
5、set函數注入bean屬性的狀況:app
AppConfig類內增長下面的代碼:yii
@Bean(name="helloBean1") public IHelloWorld helloWorld1() { HelloWorldImpl myhello= new HelloWorldImpl(); myhello.setName("Yiibai"); return myhello; }
等價於如下的XML配置:ide
<bean id="helloBean1" class="com.yiibai.core.HelloWorldImpl"> <property name="name" value="Yiibai" /> </bean>
6、無xml配置AOP函數
兩點:測試
一、AOP類加上下面的註釋
@Component @Aspect
增長AOP類---AopConfig內容以下:
package com.yiibai.config; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class AopConfig { /*Pointcut for printMsg*/ @Pointcut("execution(* *.printMsg(*))") public void printpoint() { } @Before("printpoint()") public void beforepoint() { System.out.println("接下去調用printMsg()......"); } @AfterReturning("printpoint()") public void afterprint() { System.out.println("函數printMsg()執行結束......"); } }
二、JavaConfig類加上:
@ComponentScan @EnableAspectJAutoProxy
修改後的JavaConfig類---AppConfig代碼以下:
package com.yiibai.config; 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 com.yiibai.core.HelloWorldImpl; import com.yiibai.core.IHelloWorld; @Configuration @ComponentScan @EnableAspectJAutoProxy @Import({ CustomerConfig.class, SchedulerConfig.class }) public class AppConfig { @Bean(name="helloBean") public IHelloWorld helloWorld() { return new HelloWorldImpl(); } }
運行:
7、混用XML與Java裝配方式
包com.yiibai.config下建立配置文件applicationContext.xml(本人習慣把配置文件放在包裏,這樣便於隨包總體轉移到其餘項目而沒必要修改代碼):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="helloBean" class="com.yiibai.core.HelloWorldImpl"> <property name="name" value="Yiibai" /> </bean> </beans>
用來註冊helloBean。
對AppConfig以下修改:
1)@Configuration前加上@ImportResource("classpath:com/yiibai/core/applicationContext.xml")
2)註釋helloBean的內容。
修改好的內容:
package com.yiibai.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; @ImportResource("classpath:com/yiibai/core/applicationContext.xml") @Configuration @Import({ CustomerConfig.class, SchedulerConfig.class }) public class AppConfig { }
測試代碼和運行結果都不變
(也不是徹底不變,會多出一條相似以下的消息
2017-10-10 22:45:49 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [com/yiibai/core/applicationContext.xml]
)
如須要在xml配置AOP,則applicationContext.xml內容以下:
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd "> <context:annotation-config /> <aop:aspectj-autoproxy /> <bean id="helloBean" class="com.yiibai.core.HelloWorldImpl"> <property name="name" value="Yiibai" /> </bean> <!--定義切面 --> <bean id="aopConfig" class="com.yiibai.config.AopConfig" /> </beans>
類方式注入使用aop需將<aop:aspectj-autoproxy> 改成<aop:aspectj-autoproxy proxy-target-class="true"/> 或 <aop:config proxy-target-class="true"> </aop:config>,不然會出現cannot be cast to .......錯誤.
7、注意:Web環境中使用JavaConfig,須要在web.xml中加入以下代碼:
<context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.packtpub.learnvaadin.springintegration.SpringIntegrationConfiguration</param-value> </context-param>
參考: