package com.wisely.profile;
public class DemoBean {
private String url;
public DemoBean(String url) {
super();
this.url = url;
System.out.println("地址为:"+url);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
package com.wisely.profile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class DemoConfig {
@Bean
@Profile("dev")
public DemoBean devDemoBean(){
return new DemoBean("http://www.baidu.com");
}
@Bean
@Profile("prod")
public DemoBean prodDemoBean(){
return new DemoBean("http://www.qq.com");
}
}
package com.wisely.profile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");
context.scan("com.wisely.profile");
context.refresh();
context.close();
}
}
输出结果
地址为:http://www.baidu.com
package com.wisely.profile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.wisely.profile");
context.refresh();
context.close();
}
}
地址为:http://www.qq.com
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.setInitParameter("spring.profiles.default", "dev");
}
}
原文:http://wiselyman.iteye.com/blog/2211269