标签: spring-3

使用Spring Cache的依赖关系

我想使用Spring Cache功能,但我不知道这个模块有什么依赖.我的配置如下:

<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:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    ...

<mvc:annotation-driven />
<cache:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

但是<cache:annotation-driven />没有被认可.它给出了这个错误:

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'cache:annotation-driven'
Run Code Online (Sandbox Code Playgroud)

我认为这是因为我没有Spring模块的jar文件,因为我没有添加所有(我真正需要它时添加它们).

要使Spring Cache工作,需要哪些弹簧模块罐?或者我可以在哪里找到这些信息?

谢谢

java spring dependencies caching spring-3

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

JSP表达式语言不起作用

我无法${}在我的.jsp页面上运行表达式.

displayAllCustomers.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<html>
    <body>
        <h3>Our Entire Customer Database</h3>
        <ul>
            <c:forEach items="${allCustomers}" var="customer">
                <li>${customer.name}</li>
            </c:forEach>
        </ul>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

调度员servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop 
                                      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <import resource="applicationContext.xml"/>     

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean name="/displayAllCustomers" class="mypackage.DisplayAllCustomersController">
        <property name="customerManagementService" ref="customerManagementService" />
    </bean>     

</beans>
Run Code Online (Sandbox Code Playgroud)

DisplayAllCustomersController.java

public class DisplayAllCustomersController {

    private CustomerManagementService customerManagementService;
    public void setCustomerManagementService(CustomerManagementService customerManagementService) {
        this.customerManagementService = customerManagementService; …
Run Code Online (Sandbox Code Playgroud)

jsp spring-mvc spring-3

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

status.setComplete()不清除会话

我想清理一个会话.下面是我编写的一些示例代码SessionStatus.

@Controller
@SessionAttributes(value={"sessAttr1","sessAttr2","sessAttr3"})
public class SessionController {    
@RequestMapping(value="index")
public ModelAndView populateSession(){      
    ModelAndView modelView = new ModelAndView("home");
    modelView.addObject("sessAttr1","This value is added in a session 1");
    modelView.addObject("sessAttr2","This value is added in a session 2");
    modelView.addObject("sessAttr3","This value is added in a session 3");
    return modelView;
}

@RequestMapping(value="home1")
public String populateHomeSession(SessionStatus status){
    status.setComplete();
    return "home1";
}
}
Run Code Online (Sandbox Code Playgroud)

home1显示屏幕,我仍然可以看到会话对象没有得到清除.如果我试试这个:${sessAttr1 }那么我可以在home1屏幕上阅读会话值.

请说明为什么它不起作用.

编辑

<a href="home1">Next</a>用来从一个屏幕导航到另一个屏幕.这与我面临的问题有什么关系吗?

java spring spring-mvc spring-3

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

如何在实现Condition/ConfigurationCondition接口的类中使用@Value或Environment

我只使用JavaConfig.

我有以下声明:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)

根据以下帖子,JavaConfig是必需的: 使用纯Java配置的Spring 3.2 @value注释不起作用,但Environment.getProperty可以正常工作

以下代码完美(许多@Values通过测试目的):

@Configuration
public class ActiveMQServerConfiguration {

    @Value("${localhost.address}")
    private String localHost;

    @Value("${remotehost.address}")
    private String remoteHost;

    @Value("${localhost.port}")
    private Integer localPort;

    @Value("${remotehost.port}")
    private Integer remotePort;

    @Bean(name="connectionFactory")
    @Conditional(LocalHostStatusCondition.class)
    public ActiveMQConnectionFactory localConnectionFactory(
            @Value("${localhost.protocol}") String protocol,
            @Value("${localhost.address}") String host,
            @Value("${localhost.port}") String port ){

        System.out.println("protocol: "+protocol);
        System.out.println("host: "+host);
        System.out.println("port: "+port);

        System.out.println("localHost: "+localHost);
        System.out.println("localPort: "+localPort);
        System.out.println("remoteHost: "+remoteHost);
        System.out.println("remotePort: "+remotePort);
Run Code Online (Sandbox Code Playgroud)

我可以在控制台/终端中看到

Α

protocol: tcp
host: 127.0.0.1
port: 61616
localHost: 127.0.0.1
localPort: 61616
remoteHost: …
Run Code Online (Sandbox Code Playgroud)

spring spring-3 spring-java-config spring-4

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

Spring Security java.lang.IllegalArgumentException:无法计算表达式'ROLE_ADMIN'

我正在使用" Amuthan G - Spring MVC初学者指南 " 这本书.目前我正在尝试使用下一个实现安全性security-context.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:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <security:http auto-config="true">
        <security:intercept-url pattern="/products/add" access="ROLE_ADMIN" />
        <security:form-login login-page="/login"
                             default-target-url="/products/add"
                             authentication-failure-url="/loginfailed"/>
        <security:logout logout-success-url="/logout" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="Admin" password="Admin123" authorities="ROLE_ADMIN" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>
Run Code Online (Sandbox Code Playgroud)

与安全相关的配置web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/webcontext/security-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

但是这个例子最终会IllegalArgumentException在打开安全路径时结束:

java.lang.IllegalArgumentException: Failed to evaluate expression 'ROLE_ADMIN' …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc spring-security spring-3 spring-4

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

初始化后,Spring 3.0注射为null

我试图将数据源对象注入servlet.我有使用set方法打印的记录器.它适用于pre-inialization.但是当我请求servlet时,它给了我nullPointerException.

为什么会发生这种情况的任何建议?(我不认为这与我正在扩展的servlet有关)

这是applicationContext.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="dataServlet" class="com.mycom.util.DataServlet">
    <property name="dataSource" ref="dataSource" />
    <property name="test" value="dataSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)

servlet

public class DataServlet extends DataSourceServlet {
...
@Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        logger.log(Level.INFO, "Inj: datasrc");
    }
@Autowired
    public void setTest(String test) {
        this.test = test;
        logger.log(Level.INFO, "Set Test {0}", this.test);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在setTest设置了断点,它打破了@ pre-init.但是当实际对象被请求时.它不会破坏@testTest.

为什么会这样?(单例/范围问题有关吗?)

请指教!提前致谢!

spring ioc-container spring-3

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

在控制器中注入属性值的最佳方法是什么?

这是如何从属性文件中在控制器中注入属性的最简单方法吗?然后需要在每个需要一些属性的控制器上导入属性文件.在像我这样的项目中有大约30个控制器,其中10个需要这个国家属性,我觉得它看起来很混乱.我是否理解@Value正确的使用方法?

@Controller
@RequestMapping(value = "/simple")
@ImportResource("classpath:/META-INF/properties-config.xml")
public class SimpleController {

    private @Value("#{exampleProperties['simple.country']}") String country;

}
Run Code Online (Sandbox Code Playgroud)

properties-config.xml (跳过xml和schema的东西)

<beans>
    <util:properties id="exampleProperties" location="classpath:/simple.properties" />
</beans>
Run Code Online (Sandbox Code Playgroud)

此外,当尝试在多个控制器中导入properties-config.xml资源时,我会收到此类消息.它似乎不是正确的方法,但我无法找到一个更好的..

01 Apr 2011 04:52:29,859 INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory []: Overriding bean definition for bean 'exampleProperties': replacing [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
Run Code Online (Sandbox Code Playgroud)

java spring-annotations spring-3

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

Spring 3 - 将@RequestParam值发送回视图的标准方法是什么?

我有一个简单的控制器来处理带有几个参数的提交,而不是我考虑创建一个命令对象来存储它们.

在我的控制器中,我已经注释了参数,@RequestParam但我必须将这些值发送到视图,我不知道最好的方法是什么.

如果我有一个命令对象,我可以使用modelAttributehtml:form标签绑定的参数,但我不想创建命令对象只是一堆领域.

将值发送到视图的首选方式是什么(请求属性,模型属性......)?

java data-binding spring spring-mvc spring-3

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

春天3 @Autowire参加测试

我现在有一个恼人的问题.我的测试因autowire而失败.

无法通过autowire字段:private k.dao.CompanyDao k.dao.CompanyDaoTest.companyDao; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[k.dao.CompanyDao]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者.

我觉得@ContextConfiguration可以是问题吗?

考试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:**/servlet-context.xml", "classpath:**/root-context.xml", "classpath:**/ccc-jpa.xml" })
public final class CompanyDaoTest {

    @Autowired
    private CompanyDao companyDao;

    @Test
    public void testTest() {

    }
}
Run Code Online (Sandbox Code Playgroud)

CompanyDao

public interface CompanyDao extends GenericDao<Company> {

}
Run Code Online (Sandbox Code Playgroud)

CompanyDaoJpa

@Repository("companyDao")
public class CompanyDaoJpa extends GenericDaoJpa<Company> implements CompanyDao {

    public CompanyDaoJpa() {
        super(Company.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

GenericDao

public interface GenericDao<T extends DomainObject> {

    public T get(Long id);

    public List<T> getAll();

    public T save(T object);

    public T delete(T object);

}
Run Code Online (Sandbox Code Playgroud)

servlet的context.xml中

    <annotation-driven …
Run Code Online (Sandbox Code Playgroud)

java spring-test spring-3

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

Spring 3请求处理失败; 嵌套异常是java.lang.NullPointerException

我在尝试启动我的应用程序时遇到了困难,我在几天内找到了我的错误,但我被困在代码中的某个地方并请求您的帮助谢谢

SingleTransactionsController

@Controller
public class SingleTransactionsController {
private SingleTransactionsService singleTransactionsService;


@RequestMapping(value="/disableUser/{sicil}", method=RequestMethod.GET)
public String disableUser(@PathVariable String sicil, Model model){
    singleTransactionsService.disableUser(sicil);
    model.addAttribute("message", sicil);
    return "hello";
}

}
Run Code Online (Sandbox Code Playgroud)

SingleTransactionsDAO

public interface SingleTransactionsDAO {

public void disableUser(String sicil);

}
Run Code Online (Sandbox Code Playgroud)

SingleTransactionsDAOImpl

@Repository
public class SingleTransactionsDAOImpl implements SingleTransactionsDAO{

@Override
public void disableUser(String sicil) {
    System.out.println(sicil);
}
}
Run Code Online (Sandbox Code Playgroud)

SingleTransactionsService

public interface SingleTransactionsService {
public void disableUser(String sicil);
}
Run Code Online (Sandbox Code Playgroud)

SingleTransactionsServiceImpl

@Service
public class SingleTransactionsServiceImpl implements SingleTransactionsService{

@Autowired
private SingleTransactionsDAO singleTransactionsDAO;


public void disableUser(String sicil) {
    singleTransactionsDAO.disableUser(sicil);

} …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-3

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