我有一个弹出配置注销如下:
<logout logout-url="/abc/logout"
logout-success-url="/abc/login"/>
Run Code Online (Sandbox Code Playgroud)
现在我想以编程方式注销.我如何在Spring 3中实现这一点.我需要从我的一个具有以下def的控制器中注销.目前我正在做类似以下的事......这是个好主意..
public void suppressUserProfile() {
//...
return "redirect:/abc/logout";
}
Run Code Online (Sandbox Code Playgroud) 这个例子有点人为; 我简化了它以删除无关的细节,并专注于我遇到的问题.我有一个看起来像这样的验证器:
@Component
public class UniqueUsernameValidator implements ConstraintValidator<UniqueUsername, String> {
@Autowired
UsernameService usernameService;
@Override
public void initialize(UniqueUsername uniqueUsername) {
}
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
return !usernameService.exists(s);
}
}
Run Code Online (Sandbox Code Playgroud)
我从我的控制器调用验证器,如下所示:
@RequestMapping
public void checkUsername(Model model, User user) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> constraintViolations = validator.validateProperty(user, "username");
model.addAttribute("error", constraintViolations.size() > 0);
}
Run Code Online (Sandbox Code Playgroud)
但是,我一直有NullPointerException例外.我在验证器添加一个断点,并看到usernameService了null.为什么不进行自动装配?最初我认为这是因为我没有注释验证器@Component,但即使在注释它之后我仍然有同样的问题.该UsernameService班已经标注了@Service,我可以验证它的构造函数获取调用.
我是Spring的新手,所以我甚至不确定将服务连接到验证器是否可以.我究竟做错了什么?
与早期Spring版本的问题类似,应用程序仅使用Spring 3.0依赖注入所需的最小依赖项是什么?应用程序上下文仅由XML配置.Spring依赖于日志框架,因此假设我已经包含这些JAR用于日志记录:
在Spring和Spring Security 3.1中以特定用户名编程登录Web访问者的正确方法是什么?看起来我在2.5下做的方式已经改变了一点.我相信现在有更好的方法可以做到这一点.
基本上,当我创建一个新用户时,我还需要同时登录它们.
通常我会添加这样org.springframework.web.filter.DelegatingFilterProxy的代码片段到web.xml:
<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)
但是使用Servlet 3.0 Container和Jetty,我删除了web.xml.我正在尝试将DelegatingFilterProxy添加到Jetty的启动中:
context.addFilter(DelegatingFilterProxy.class, "/*", EnumSet.allOf(DispatcherType.class));
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
No bean named 'org.springframework.web.filter.DelegatingFilterProxy-100555887' is defined
Run Code Online (Sandbox Code Playgroud)
我该如何创建和添加此过滤器?
我有春豆.
public class Employee2 {
private int id;
private String name;
private double salary;
public Employee2(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
// some logic to call database using above values
}
Run Code Online (Sandbox Code Playgroud)
现在我在spring配置文件中有以下配置.
<bean id="emp2" class="com.basic.Employee2">
<constructor-arg name="id" value="" />
<constructor-arg name="name" value="" />
<constructor-arg name="salary" value="" />
</bean>
Run Code Online (Sandbox Code Playgroud)
现在我不能硬编码上面配置中的值,因为它们是动态的.
现在我使用下面的代码以编程方式获取spring bean.豆的范围是singelton.
Employee2 emp = (Employee2)applicationContext.getBean("emp2");
Run Code Online (Sandbox Code Playgroud)
现在我如何将值传递给Employee2构造函数?
谢谢!
在spring security 3.0中,我们正在使用AuthenticationProcessingFilter类,我们使用的determineTargetUrl()方法是根据不同的角色返回url.
现在,我们正在转向spring security 3.1.0.RC3,我现在应该如何确定基于不同角色的url,因为AuthenticationProcessingFilter类已从新版本中删除.任何人都可以请给我一些简短的代码,以便我可以实现自定义过滤器重定向到不同角色的不同页面.
我需要帮助理解背后的理念@Autowired和@Service.我有一个DAO @Service和控制器定义,@Autowired一切似乎很好,但是,我@Autowired在不同的类使用相同,然后它不起作用.
例:
服务
@Service
public class MyService {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource (DataSource myDataSource) {
this.jdbcTemplate = new JdbcTemplate(myDataSource);
}
public void testUpdate(){
jdbcTemplate.update("some query");
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
package com.springtest.mywork.controller;
@Controller
@RequestMapping(value = "/test.html")
public class MyController
{
@Autowired
MyService myService;
@RequestMapping(method = RequestMethod.GET)
public String test(Model model)
{
systemsService.testUpdate();
return "view/test";
}
}
Run Code Online (Sandbox Code Playgroud)
以上一切都很好.但是,如果我想MyService在POJO中使用那么它就不起作用了.例:
package com.springtest.mywork.pojos;
public class MyPojo {
@Autowired
MyService myService;
public …Run Code Online (Sandbox Code Playgroud) I am working on a Spring application on Tomcat7, JDK1.7, Maven and other components. Recently, I made a major change to the application, requiring switching over to Spring 3. After the change, I'm seeing the below exception on deploying to dev server. The application runs flawlessly on my local system though.
javax.servlet.ServletException: Servlet.init() for servlet amadeusAce threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)
root cause
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/amadeusAce-servlet.xml]; …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种更清晰的方式(在Spring 3.2中)获取当前语言环境,而不是在每个Controller方法的开头显式调用LocaleContextHolder.getLocale().它必须与Java注释兼容,因为我没有使用XML配置.这就是我目前正在做的事情.
@Controller
public class WifeController {
@Autowired
private MessageSource msgSrc;
@RequestMapping(value = "/wife/mood")
public String readWife(Model model, @RequestParam("whatImDoing") String iAm) {
Locale loc = LocaleContextHolder.getLocale();
if(iAm.equals("playingXbox")) {
model.addAttribute( "statusTitle", msgSrc.getMessage("mood.angry", null, loc) );
model.addAttribute( "statusDetail", msgSrc.getMessage("mood.angry.xboxdiatribe", null, loc) );
}
return "moodResult";
}
}
Run Code Online (Sandbox Code Playgroud) spring-3 ×10
spring ×6
java ×4
spring-mvc ×4
autowired ×2
jetty ×1
locale ×1
maven ×1
pom.xml ×1
servlet-3.0 ×1
tomcat7 ×1
validation ×1