ApplicationContextInitializer 和 BeanFactoryPostProcessor 有什么区别?什么时候实现什么接口?
Spring有多种*Aware接口,例如。ApplicationContextAware向实现者添加一个设置者。与简单地通过常规 DI 方式(例如构造函数注入)请求依赖项相比,使用这些接口是否有任何好处?
换句话说,我应该什么时候选择
@Service
class MyService implements ApplicationContextAware {
private ApplicationContext applicationContext;
void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Run Code Online (Sandbox Code Playgroud)
超过
@Service
class MyService implements ApplicationContextAware {
private ApplicationContext applicationContext;
public MyService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
Run Code Online (Sandbox Code Playgroud)
或者
@Service
class MyService implements ApplicationContextAware {
@Autowired
private ApplicationContext applicationContext;
}
Run Code Online (Sandbox Code Playgroud)
?
spring-boot 中有两个 IOC 容器:BeanFactory和ApplicationContext。
根据我的理解,ApplicationContext支持bean的急切初始化,在哪里BeanFactory延迟初始化。
问题陈述:在我的 Spring boot 应用程序中,我想使用 bean 的延迟初始化来使应用程序启动更快。任何人都可以建议实现相同目标的解决方案吗?
java performance lazy-initialization applicationcontext spring-boot
我有一些我创建的bean,它们都使用类似的模式进行bean实例化.顶级对象都非常相似,但它们包含的对象因字符串构造函数参数而异.除了两个实例THIS CHANGES A和一个实例之外,每个顶级bean中的所有内容都是相同的THIS CHANGES B.下面是我的一个豆子.除了THIS CHANGES值之外,其他的完全相同.
<bean id="mover1" class="CustomDataMover">
<constructor-arg ref="session"/>
<constructor-arg>
<bean class="DataCache">
<constructor-arg>
<bean class="AllValuesReader">
<constructor-arg ref="databaseConnector"/>
<constructor-arg value="THIS CHANGES A"/>
<constructor-arg value="v1"/>
<constructor-arg value="v2"/>
</bean>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg ref="customUpdate"/>
<constructor-arg value="THIS CHANGES B"/>
<constructor-arg>
<bean class="ValueGenerator">
<constructor-arg>
<bean class="LatestValueRetriever">
<constructor-arg ref="databaseConnector"/>
<constructor-arg value="v3"/>
<constructor-arg value="v4"/>
<constructor-arg value="THIS CHANGES A"/>
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
如何减少我的bean中的重复量?我正在寻找一些方法来制作某种模板.此外,请注意我确实有其他bean的参考.
我正在使用eclipse juno IDE我有Java应用程序,它有src文件夹.在我的文件夹中:
1)applicationContext.xml
2)persistence.xml
我也有DBInterface,我用JPA实现它.现在在applicationContext.xml文件中,我有一个用于JPA实现的bean.
当我尝试注入bean时,我得到了一个类似"找不到持久性提供程序"的删除.
所以我尝试在applicationContext文件中导入持久性文件,但是我得到了另一个例外.
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:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd
http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd
http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd">
<bean id="JPA" class="pack.jpa.JPAQueries"/>
<import resource="persistence.xml"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
persistence.xml中
<?xml version="1.0" encoding="UTF-8"?>
<persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit transaction-type="RESOURCE_LOCAL" name="MyJPA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>pack.bl.Travels</class>
<class>pack.bl.Example</class>
<properties> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/taxis"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> …Run Code Online (Sandbox Code Playgroud) 快问.当你在webApp使用的另一个jar中时,你可以在applicationContext.xml中引用Spring类吗?
JAR(包含我所有服务和daos等的常见jar)在WAR文件中,但是当我尝试通过applicationContext.xml引用服务时,我收到以下错误: -
Error creating bean with name 'com.myproject.common.test.impl.TestServiceImpl' defined in ServletContext resource [/WEB-INF/context/spring-context.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
Run Code Online (Sandbox Code Playgroud)
(注意spring-context.xml被导入applicationContext.xml而没有错误.)
我的上下文XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="com.myproject.common.test.impl.TestServiceImpl">
<property name="genericDao" ref="genericDao" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的应用程序包都在com.myproject.web下我的常见JARS都在com.myproect.common下
我的java应用程序使用Spring构造型注释(@Controller,@ Component)和autowire注释来管理依赖注入.
它不是Web应用程序,只是简单的jar.它也是基于纯注释的代码,即根本没有xml.
什么是从main方法初始化基于Spring注释的应用程序上下文和默认配置的正确方法?
#所有请阅读我的问题倾斜我已经解决了这个问题
Spring 循环参考在 Local 、 UAT 、 DEMO 、 STAGGING 环境中工作,但不仅限于生产环境。所有环境都具有相同的配置
1 JDK:1.7.0.79
2.Tomcat 7.0
应用上下文.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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="in.test.server">
</context:component-scan>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="applicationProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:mail.properties</value>
<value>classpath*:document_storage.properties</value>
<value>classpath*:ekyc.properties</value>
<value>classpath*:application_config.properties</value>
<value>classpath*:messages.properties</value>
<value>classpath*:invitation_codes.properties</value>
<value>classpath*:capitalraise.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:mail.properties</value>
<value>classpath*:application_config.properties</value>
</list>
</property>
</bean>
<bean …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Datasource 实例注册为 java 代码中的 bean(spring-boot 项目)
这是我写的。(此代码不起作用。)
@Configuration
public class DatabaseConfig {
private Logger logger = Logger.getLogger(DatabaseConfig.class);
@Autowired
ApplicationContext context;
private Map<String, Map<String, String>> dsMap;
private Map<String, String> getTestDataSourceInfo () {
Map<String, String> ds = new HashMap<String, String> ();
ds.put("driverClassName", "com.mysql.jdbc.Driver");
ds.put("url", "jdbc:mysql://123.456.78.912:3306/test");
ds.put("username", "testuser");
ds.put("password", "testuser");
return ds;
}
public DatabaseConfig () {
this.dsMap = new HashMap<String, Map<String, String>>();
dsMap.put("sampleDs", getTestDataSourceInfo());
}
@PostConstruct
public void loadDataSource () {
logger.info("DS ================================ :: " + String.valueOf(this.dsMap));
this.dsMap.forEach((k,v) -> {
logger.info("value …Run Code Online (Sandbox Code Playgroud) 如何在每次测试执行后使用 Junit5 和 Spring Boot 清除应用程序上下文?我希望在测试中创建的所有 bean 在执行后都被销毁,因为我在多个测试中创建了相同的 bean。我不想为所有测试使用一个配置类,而是每个测试都有一个配置类,如下所示。
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = MyTest.ContextConfiguration.class)
public class MyTest{
...
public static class ContextConfiguration {
// beans defined here...
}
}
Run Code Online (Sandbox Code Playgroud)
Putting@DirtiesContext(classMode = BEFORE_CLASS)不适用于 Junit5。
spring ×8
java ×5
spring-boot ×2
annotations ×1
javabeans ×1
jpa ×1
junit ×1
junit5 ×1
mapping ×1
performance ×1
persistence ×1
spring-bean ×1
spring-test ×1
tomcat7 ×1