我有一个包(比方说packagesToScan)包含我希望持久注释的类@Entity.
在定义ApplicationContext配置时,我做了如下操作.
@Configuration
@EnableJpaRepositories("packagesToScan")
@EnableTransactionManagement
@PropertySource("server/jdbc.properties")
@ComponentScan("packagesToScan")
public class JpaContext {
...
// Other configurations
....
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(this.dataSource());
emf.setJpaVendorAdapter(this.jpaVendorAdapter());
emf.setPackagesToScan("packagesToScan");
emf.setJpaProperties(this.hibernateProperties());
return emf;
}
Run Code Online (Sandbox Code Playgroud)
在开发时,我有一些类packagesToScan不满足持久性要求(比如没有主键等),因此我不允许因为ApplicationContext安装失败而运行测试.
现在,
有什么方法可以扫描一些选定的类或忽略其中的一些类packagesToScan?
在Spring 3发行版中,有一个项目文件夹包装在发行版中.此项目文件夹applicationContext.xml包含可以使用的示例文件.但是,当我从这里下载Spring 4发行版时,它没有项目文件夹,我无法找到样本applicationContext.xml.我在哪里可以找到示例applicationContext.xml文件.
我正在尝试使用具有分层应用程序上下文的spring boot创建应用程序.我目前的主要方法如下:
public static void main(String[] args) {
new SpringApplicationBuilder(TestApplication.class)
.child(AuditServiceConfiguration.class).web(true)
.child(TaskServiceConfiguration.class).web(true)
.run(args);
}
Run Code Online (Sandbox Code Playgroud)
并且两个子配置注释为:
@EnableAutoConfiguration
@Configuration
Run Code Online (Sandbox Code Playgroud)
我们的想法是让父上下文包含所有公共bean和每个子上下文,以便在与其兄弟姐妹隔离时运行自己的MVC.
不幸的是,当我运行上面的命令时,只会初始化并启动最后一个子上下文.
任何指向正确方向的人都会非常感激.
问候,
亚历山德罗
有没有人知道Spring是否有任何扩展允许通过JSON(或其他任何格式)而不是XML配置其ApplicationContext?我在官方文档中找不到任何内容,但我想知道是否有任何其他开源扩展可以允许这样做.
为了清楚起见,我不是在谈论配置SpringMVC来设置基于RESTful JSON的Web服务或类似的东西,只是可以通过JSON而不是XML来进行Spring应用程序配置.
我需要在我的测试类的单个方法中更改我的applicationContext中活动的Spring配置文件,为此我需要在刷新竞赛之前运行一行代码,因为我使用的是ProfileResolver.我尝试过以下方法:
@WebAppConfiguration
@ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"})
@ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class)
public class ControllerTest extends AbstractTestNGSpringContextTests {
@Test
public void test() throws Exception {
codeToSetActiveProfiles(...);
((ConfigurableApplicationContext)this.applicationContext).refresh();
... tests here ...
codeToSetActiveProfiles(... back to prior profiles ...);
... ideally refresh/reload the context for future tests
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
Run Code Online (Sandbox Code Playgroud)
DirtiesContext对我来说不起作用,因为它是在类/方法执行之后运行,而不是之前,我需要在运行刷新/重新加载之前执行一行代码.
有什么建议?我试着看一下正在运行的监听器/钩子,但是我没有看到一个明显的位置来插入自己来实现这种行为.
我正在攻读Spring Core认证,并且根据提供的学习内容,我有这个问题,但我无法给出答案.
为什么必须在@Configuration注释类中使用默认构造函数?
我没有将任何构造函数声明为@Configuration批注注释的配置类.默认构造函数是超类继承的构造函数?或者是什么?为什么我必须有一个默认的构造函数,我不能覆盖它?
TNX
注意:我提到的测试项目可以下载:
git clone https://github.com/mperdikeas/so-spring-di-appcontext-schemalocation.git
..并使用'ant run'运行.
我理解'XML命名空间名称仅用作不透明标识符,并不打算用作URI(维基百科).我还"理解"XML模式位置旨在提供关于模式文档的实际位置的提示,并且是提示,而不是在实践中使用(w3.org).考虑到这一点,我一直在通过修改applicationContext.xml来试验一个简单的Spring DI应用程序(在简单的J2SE设置中使用).这是起始版本:
<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: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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="atm"/>
<context:property-placeholder location="classpath:META-INF/spring/atm.properties"/>
<bean id="soapTransport_" class="atm.SoapATMTransport" p:retries="${transport.retries}"/>
Run Code Online (Sandbox Code Playgroud)
当我执行'sudo ifconfig eth0 down'时,项目运行得很好,这与运行时没有从schemaLocations中获取任何内容一致.但是,当我通过向每对中的第二个URL添加一个简单的下划线来修改schemaLocations时,我收到了以下投诉:
[java] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [META-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 100; cvc-elt.1: Cannot find the declaration of element 'beans'.
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221) …Run Code Online (Sandbox Code Playgroud) 以下哪种方式更适合获取Application的实例
在 Application.onCreate() 中初始化静态字段并提供对它的静态访问
public class MyApplication extends Application {
private static MyApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public static MyApplication getInstance() {
return MyApplication.sInstance;
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyApplication application = MyApplication.getInstance();
}
}
Run Code Online (Sandbox Code Playgroud)创建以 Context 作为参数的静态方法并将该 Context 转换为 MyApplication
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
public static MyApplication getInstance(Context context) {
return ((MyApplication) …Run Code Online (Sandbox Code Playgroud)嗨,我不明白有什么优点可以让我使用@ContextHierarchy,如下所示:
@ContextHierarchy({
@ContextConfiguration("/test-db-setup-context.xml"),
@ContextConfiguration("FirstTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class FirstTest {
...
}
@ContextHierarchy({
@ContextConfiguration("/test-db-setup-context.xml"),
@ContextConfiguration("SecondTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class SecondTest {
...
}
Run Code Online (Sandbox Code Playgroud)
过度使用带有locations参数的单个@ContextConfiguration,如下所示:
@ContextConfiguration(locations = {"classpath:test-db-setup-context.xml", "FirstTest-context.xml", "SecondTest-context.xml" })
Run Code Online (Sandbox Code Playgroud)
在每种情况下,应用程序上下文在不同的junit测试类之间共享.
spring ×9
java ×3
spring-mvc ×2
android ×1
annotations ×1
hierarchical ×1
json ×1
junit ×1
spring-boot ×1
spring-data ×1
spring-test ×1
testng ×1
xml ×1