我有一个由注释和类配置的Spring(mvc和安全)应用程序。没有额外的xml和这样的
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.nixsolutions.servicestation.service.impl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
web.xml中。现在我有创建泽西休息网络服务的任务。 我添加了这样的依赖
<<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>2.22.1</version>
</dependency>
现在我在部署时失败了:
No annotated classes found for specified class/package [classpath:applicationContext.xml]
如果我删除jersey-spring3部署将成功完成。但后来我的网络服务不起作用。正如我所读到的,没有该库的bean没有映射。 如果有可能完成项目而不将Spring减少到版本3并使用映射创建所有这些xml?
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.nixsolutions.servicestation")
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.nixsolutions.util"})
@PropertySource(value = { "classpath:springhiber.properties" })
public class HiberUtil {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.nixsolutions.servicestation.entity" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl(environment.getRequiredProperty("JDBC_URL"));
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.format_sql", "false");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.hbm2ddl.import_files", "import.sql");
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
@EnableWebSecurity
@Configuration
@ComponentScan(value = "com.nixsolutions")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("SELECT login AS username, password AS password, 'true' " +
"FROM user WHERE login = ?")
.authoritiesByUsernameQuery("SELECT u.login AS username, " +
"CASE r.role_name WHEN 'manager' THEN 'ROLE_ADMIN' " +
"ELSE 'ROLE_USER' END AS role FROM user u " +
"INNER JOIN role r ON u.role_id = r.role_id WHERE u.login = ?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/homepage").access("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
.antMatchers("/cars", "/clients", "/orders", "/workers").access("hasRole('ROLE_ADMIN')")
.and().formLogin()
.loginPage("/")
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("login")
.passwordParameter("password")
.defaultSuccessUrl("/workers")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/homepage");
}
}
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SecurityConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
securityFilter.addMappingForUrlPatterns(null, false, "/*");
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}