- 不像 annotation 還會用到一些 XML,JavaConfig 完全免除 XML
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
// 也可以慢慢註冊
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
// 也可以用掃描的
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("idv.neil.java119");
ctx.refresh();
- 當然一定會有 web 版本:
<web-app>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- 可用逗號或空白指定多個 package -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- 可用逗號或空白指定多個 package -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
</web-app>
- @Import
- 同一個 Java class 的 inject:
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar()); // 直接呼叫 method 就可以了
}
@Bean
public Bar bar() {
return new Bar();
}
}
- 不同 Java class 的 inject,使用@Autowired:
@Configuration
public class ServiceConfig {
private @Autowired AccountRepository accountRepository;
public @Bean TransferService transferService() {
return new TransferServiceImpl(accountRepository);
}
}
@Configuration
public class RepositoryConfig {
private @Autowired DataSource dataSource;
public @Bean AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
}
@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class SystemTestConfig {
public @Bean DataSource dataSource() { }
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// ...
}
- JavaConfig 還是沒辦法完全取代 XML,這時可以用 @ImportResource 載入 XML 檔。
- Lifecycle callbacks
- Foo 裡要有 init 這個 method,Bar 裡要有 cleanup 這個 method。
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}
- 也可以直接呼叫,使用 JavaConfig 的好處是可以使用 java 的方式做任何事情,而不用依靠 Spring container。
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
Foo foo = new Foo();
foo.init();
return foo;
}
}
- @Scope
- 預設是 singleton。
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
}
}
- Scope proxy
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserPreferences userPreferences() {
return new UserPreferences();
}
- Lookup method inject
@Bean
public CommandManager commandManager() {
return new CommandManager() {
protected Command createCommand() {
return asyncCommand();
}
}
}
- 自訂 bean id
- 神奇的 singleton 效果
沒有留言:
張貼留言