标签: applicationcontext

应用程序上下文是否在Spring Framework中特定于用户或应用程序?

您从.xml文件加载的应用程序上下文是用户特定级别还是应用程序级别,例如上下文是否已为所有用户加载?或者每个用户都有单独的应用程

在java中使用spring框架?

java spring applicationcontext

1
推荐指数
1
解决办法
930
查看次数

错误:元素"security:authentication-provider"的前缀"security"未绑定

这是我的application-security.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!--
  - Sample namespace-based configuration
  -
  - $Id: applicationContext-security.xml 3019 2008-05-01 17:51:48Z luke_t $
  -->
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<global-method-security secured-annotations="enabled">
</global-method-security>
Run Code Online (Sandbox Code Playgroud)

    <!-- Don't set any role restrictions on login.jsp -->
    <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />

    <!-- Restrict access to ALL other pages -->
    <intercept-url pattern="/**" access="ROLE_USER" />
    <intercept-url pattern="/admin/*.html" access="ROLE_ADMIN"  />
    <intercept-url pattern="/manager/*.html" access="ROLE_MANAGER"  />
    <intercept-url pattern="/**.html" access="ROLE_USER,ROLE_ADMIN, ROLE_MANAGER"  />
    <!-- Set the login page and what to do if login …
Run Code Online (Sandbox Code Playgroud)

spring spring-security applicationcontext

1
推荐指数
1
解决办法
5688
查看次数

使用spring进行Java注释扫描

我有几个类需要用名称注释,所以我将我的注释定义为

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface JsonUnmarshallable {
    public String value();
}
Run Code Online (Sandbox Code Playgroud)

现在需要此注释的类被定义为

@JsonUnmarshallable("myClass")
public class MyClassInfo {
<few properties>
}
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码来扫描注释

private <T> Map<String, T> scanForAnnotation(Class<JsonUnmarshallable> annotationType) {
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(applicationContext, false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(annotationType));
    scanner.scan("my");
    applicationContext.refresh();
    return (Map<String, T>) applicationContext.getBeansWithAnnotation(annotationType);
}
Run Code Online (Sandbox Code Playgroud)

问题是返回的映射包含["myClassInfo" -> object of MyClassInfo]但我需要映射包含"myClass"为键,这是Annotation的值而不是bean名称.

有办法做到这一点吗?

java spring annotations applicationcontext

1
推荐指数
2
解决办法
6717
查看次数

是否可以在applicationcontext中设置资源属性值?

在applicationcontext.xml中,是否可以设置一个值,以后可以在SPEL表达式中使用?

例如,有没有办法做到这一点?:

<setProperty name="foo" value="someval" />


<bean id="beanId" name="beanName" class="SomeClass">
    <property name="someVal" value="blah_${foo}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

我想要这样做的实际原因是我使用语句来创建在许多不同的应用程序上下文中使用的实体管理器.问题是实体管理器需要一个唯一的名称,Bitronix使用该名称来创建一个本地文件,如果多个单元测试同时使用该字段的相同名称运行,则该文件会中断.要设置该唯一名称,我当前为每个应用程序上下文都有一个单独的属性文件,并导入它以从中获取唯一的名称.

而不是做那些废话,我宁愿这样做:

<setProperty name="uniqueName" value="someUniqueName" />
<import resource="classpath*:shared/db/fooDb.xml" />
Run Code Online (Sandbox Code Playgroud)

spring applicationcontext

1
推荐指数
1
解决办法
1987
查看次数

Spring FileSystemXmlApplicationContext找不到bean配置文件并进入错误状态

我正在尝试执行以下Spring"Hello World"示例,该示例使用FileSystemXmlApplicationContext作为ApplicationContext接口的实现.

此实现必须将XML bean配置的完整路径作为构造函数参数,类似于前面的示例:

ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
Run Code Online (Sandbox Code Playgroud)

我使用Linux和我的豆子的配置文件是以下路径: /home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml,所以在我的代码有:

ApplicationContext context = new FileSystemXmlApplicationContext("/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml");
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试运行我的应用程序时,在STS\Eclipse控制台中,我有以下错误消息(似乎找不到该文件):

INFO: Loading XML bean definitions from file [/home/andrea/Documents/ws/myapplicationcontextexample/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/home/andrea/Documents/ws/myapplicationcontextexample/home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml]; nested exception is java.io.FileNotFoundException: home/andrea/Documents/ws/myapplicationcontextexample/src/main/java/Beans.xml (File o directory non esistente)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc applicationcontext

1
推荐指数
1
解决办法
9275
查看次数

为什么SpringBatch MultiResourcePartitioner无法识别jobParameters?更重要的是,该怎么办呢?

这是一个非常简单的设置.我有一个潜在的5m记录的文件,我需要阅读并做一些工作,然后发送到数据库.处理和写作的机制并不重要.我需要能够将路径和文件名[/opt/etc/app/partner/input_file.csv]作为参数传递给进程.这很容易,将它添加到JobParameters并将其提供给JobLauncher.

JobParametersBuilder jpBuilder = new JobParametersBuilder() ;
jpBuilder.addString("filePath", "/opt/etc/app/partner/input_file.csv") ;
jobLauncher.run(job, jpBuilder.toJobParameters() ;
Run Code Online (Sandbox Code Playgroud)

完成,现在让上下文意识到它.再次简单的引用jobParameters问题.

<?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:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <description>PoC to demonstrate variable row content handling</description>

    <batch:job id="poc" job-repository="jobRepository" incrementer="runIdIncrementer" restartable="true">
        <batch:step id="pocReadWriteStep">
            <batch:partition step="step" partitioner="partitioner">
                <batch:handler task-executor="taskExecutor"/>
            </batch:partition>
        </batch:step>
    </batch:job>

    <batch:step id="step">
        <batch:tasklet task-executor="taskExecutor" throttle-limit="20" transaction-manager="transactionManager" allow-start-if-complete="true">
            <batch:transaction-attributes isolation="READ_UNCOMMITTED"/>
            <batch:chunk
                reader="reader"
                processor="processor"
                writer="writer"
                commit-interval="20">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="file:#{jobParameters['filePath']}"/>
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
                <property name="tokenizers">
                    <map>
                        <entry …
Run Code Online (Sandbox Code Playgroud)

java batch-processing spring-batch applicationcontext

1
推荐指数
1
解决办法
3051
查看次数

如何在需要WPF应用程序时进行单元测试?

我需要使用单元测试来测试我的库.

不幸的是,该库将System.Windows.Application实例作为参数.通常在现实世界的WPF应用程序中,这将是public partial class App : Application入口App.xaml应用程序类.

这是DispatcherUnhandledException库用于注册处理程序的事件所必需的.

有没有办法模拟或初始化Application.Current,在WPF单元测试库中为null?

谢谢.

c# wpf xaml unit-testing applicationcontext

1
推荐指数
1
解决办法
3211
查看次数

@Controller不再使用spring.data.rest.base-path变量用于@RequestMapping的REST URL

我使用变量set in 为正确设置REST URL的@RestController所有@Entity对象创建了一个对象,但是它没有使用变量.spring.data.rest.base-pathapplication.peroperties/api@RequestMapping("someEndpoint")

例

对于@Entity类User,REST端点位于:

`http://localhost:8081/api/users'
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问时someEndpoint:

'http://localhost:8081/api/someEndpoint'
Run Code Online (Sandbox Code Playgroud)

我收到的答复是:

响应状态

HTTP/1.1 404 Not Found
Run Code Online (Sandbox Code Playgroud)

身体

"timestamp":1461267817272,"status":404,"error":"Not Found","message":"No message available","path":"/api/someEndpoint"}
Run Code Online (Sandbox Code Playgroud)

相反,REST服务的端点位于

'http://localhost:8081/someEndpoint'
Run Code Online (Sandbox Code Playgroud)

响应:

HTTP/1.1 200 OK
Run Code Online (Sandbox Code Playgroud)

控制器类

@RestController
public class HomeController {

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }

    @RequestMapping("someEndpoint")
    public Stuff runSomething(
            @RequestParam(value = "id", required = true) String id)
Run Code Online (Sandbox Code Playgroud)

我的配置中缺少什么?

谢谢

rest spring spring-data applicationcontext spring-data-rest

1
推荐指数
1
解决办法
2166
查看次数

在Spring中,尽管已经定义了"PropertyPlaceholderConfigurer",但获取"java.lang.IllegalArgumentException:无法解析占位符"

我正在使用Spring 3.2.11.RELEASE和Maven 3.3.我在我的应用程序上下文文件中定义了这个...

<bean id="localPropertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
        <list>
            <value>classpath:quickbase.properties</value>
        </list>
        </property>
</bean> 
…
<bean id=“myClient" class="org.mainco.subco.mysystem.MyClient">
    <constructor-arg index="0" type="String" value="${quickbase.username}" />
    <constructor-arg index="1" type="String" value="${quickbase.password}" />
    <constructor-arg index="2" type="String" value="${quickbase.url}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的测试时,我得到以下错误

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myClient' defined in class path resource [META-INF/spring/applicationContext-orders.xml]: Could not resolve placeholder 'quickbase.username' in string value "${quickbase.username}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'quickbase.username' in string value "${quickbase.username}"
Run Code Online (Sandbox Code Playgroud)

这让我感到困惑,因为在我的target/classes目录中,我可以看到一个文件"quickbase.properties",它定义了"quickbase.username".我无法弄清楚我还需要检查什么.

spring properties applicationcontext property-placeholder

1
推荐指数
1
解决办法
1万
查看次数

在同一上下文中通过Spring Boot创建的对象的两个不同哈希码

我编写了一个简单的Spring Boot应用程序,稍后我将扩展它以构建Spring REST客户端.我有一个工作代码; 我正在玩弄更好地理解这些概念.代码如下.

@SpringBootApplication
public class RestClientApplication {

public static void main(String[] args) {
    SpringApplication.run(RestClientApplication.class, args);

    try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
            RestClientApplication.class)) {
        System.out.println(" Getting RestTemplate : " + ctx.getBean("restTemplate"));
    }
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
}

@Bean
public CommandLineRunner run(RestTemplate template) {
    return args -> {
        System.out.println("Rest Template instance from CLR is : " + template);
    };
}

}
Run Code Online (Sandbox Code Playgroud)

意见

Rest Template instance from CLR is : org.springframework.web.client.RestTemplate@1e53135d
Getting RestTemplate : org.springframework.web.client.RestTemplate@5aa6202e
Run Code Online (Sandbox Code Playgroud)

问题 …

spring applicationcontext spring-boot

1
推荐指数
1
解决办法
37
查看次数