Q: 在Web应用的web.xml文件中设置默认的profile
A:
<?xmlversion="1.0"encoding="utf-8"?>
<web-app version=2.5
xmins="http://java.suncom/xmi/ns/javaee
xmins:xsi=http://www.w3.org/2001/xmlschema-instance
xsischemalocation="http://java.suncom/xml/ns/javaee
http://java.suncom/xml/ns/javaee/web-app_2-5.xsd">
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring/root-contextxml</param-value></context-param>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name> <------- 为上下文设置默认的profile
<param-value>dev</param-value>
</context-param>
<listener>
<1istener-class>
org.springframeworkwebcontext.Contextloaderlistener
</1istener-class>
</listener>
<servlet>
<servlet-name>appservlet</servlet-name>
<servlet-class>
org.springframework.web.servletDispatche
</servlet-class>
<init-param>
<param-name>spring.profiles.default</param-hame> <------- 为Servlet设置默认的profile
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Q: 重复使用元素来指定多个profile
A:
<?xml version="1.0" encoding="utf-8"?>
<beans xmins="http://www.springframework.org/schema/beans"
xmins:xsi=http://www.w3.org/2001/xmlschema-instance"
xmins:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee=http://www.springframework.org/schema/jee"
xmins:p="http://www.springframework.org/schema/p"
xsi:schemalocation="
http://www.springframeworkorg/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-dbc.xsd
http://www.springframeworkorg/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans profile="dev"> <------- dev profile的bean
<jdbc:embedded-database id="datasource">
<jdbc:scriptlocation="classpath:schema.sql"/>
<jdbc:scriptlocation="classpath:test-data.sql"/>
</jdbc:embedded-database>
</beans>
<beans profile="qa"> <------- qa profile的bean
<bean id="datasource"
class="org.apache.commons.dbcp.Basicdatasource"
destroy-method="close"
p:url="jdbc:h2:tcp://dbserver/~/test"
p:driverClassName="org.h2.Driver"
p:username="sa"
p:password="password"
p:initialsize="20"
p:maxactive="30"/>
</bean>
<bean profile="prod"> <------- prod profile的bean
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/myDatabase"
resourcce-ref="true"
proxy-interface="javax.sql.DataSource" />
</bean>
<beans>
Q: @Profile注解基于激活的profile实现bean的装配
A:
package com.myapp;
import javax.sql.DataS0urce;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBui1der;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jndi.JndiObjectFactoryBean;
@configuration
public class DataSourceConfig {
@Bean(destroyMethod="shutdown")
@Profi1e("dev") <------- 为 dev profile 装配的bean
public DataSource embeddedDataSource ) (
return new EmbeddedDatabaseBui1der()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sg1")
.addScript("classpath:test—data.sg1")
.build();
}
@Bean
@profile("prod") <------- 为 prod profile 装配的bean
public DataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/myDS");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource. class);
return (DataSource) jndiObjectFactoryBean.getObject();
}
}
Q: 在Spring中装配bean的三种主要方式
A: 自动化配置、基于Java的显式配置以及基于XML的显式配置
Q: 我们该如何让Spring同时加载它和其他基于Java的配置呢?
A: 答案是@ImportResource注解
Q: p-命名空间属性是如何组成的。
A:
属性名 所注入的bean的ID
+---------+ +-----------+
+ + + +
P:compactDisc-ref="compactDisc"
^ | |
| | |
| | |
+ +--+
p-命名空间前缀 注入bean引用
Q: 选择构造器注入还是属性注入时的通用规则
A: 倾向于对强依赖使用构造器注入,而对可选性的依赖使用属性注入。
Q: 在XML中声明DI时,会有多种可选的配置方案和风格。具体到构造器注入,有两种基本的配置方案可供选择:
A:
Q: 在Spring XML配置中,只有一种声明bean的方式
A: 使用元素并指定class属性。Spring会从这里获取必要的信息来创建bean。
Q: JavaConfig要优于XML配置的部分原因。
A: