spring-boot-jpa与shiro整合,但是我的entity和jpa不在同一工程下?

osBwju 发布于 2016/07/13 14:57
阅读 985
收藏 0

@terrymanu 你好,想跟你请教个问题:

配置文件:


package com.vanxd.admin.start;

import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.vanxd.admin.shiro.realm.SysUserRealm;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wejoy-a on 2016/6/27.
 */
@EnableAutoConfiguration
@EntityScan(basePackages = "com.vanxd")
@EnableJpaRepositories(basePackages= { "com.vanxd" })
@ComponentScan(basePackages = "com.vanxd")
@ConditionalOnClass({FastJsonHttpMessageConverter.class})
@Configuration
public class Start extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter{
    public static void main(String[] args) {
        SpringApplication.run(Start.class);
    }

    @Bean
    @ConditionalOnMissingBean({FastJsonHttpMessageConverter.class})
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        ValueFilter valueFilter = new ValueFilter() {
            /**
             * 对特定的值进行处理
             *
             * @param o 类
             * @param key 键
             * @param value 值
             * @return
             */
            public Object process(Object o, String key, Object value) {
                if (null == value){
                    value = "";
                }
                return value;
            }
        };
        fastJsonConfig.setSerializeFilters(valueFilter);
        converter.setFastJsonConfig(fastJsonConfig);
        return converter;
    }

    @Bean
    public EhCacheManager getEhCacheManager() {
        EhCacheManager em = new EhCacheManager();
        em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
        return em;
    }

    @Bean(name = "myShiroRealm")
    public SysUserRealm myShiroRealm(EhCacheManager cacheManager) {
        SysUserRealm realm = new SysUserRealm();
        realm.setCacheManager(cacheManager);
        return realm;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        daap.setProxyTargetClass(true);
        return daap;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(SysUserRealm sysUserRealm) {
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
        dwsm.setRealm(sysUserRealm);
        dwsm.setCacheManager(getEhCacheManager());
        return dwsm;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
        aasa.setSecurityManager(securityManager);
        return aasa;
    }

    private void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean){
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/user", "authc");
        filterChainDefinitionMap.put("/user/edit/**", "authc,perms[user:edit]");

        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/**", "anon");//anon 可以理解为不拦截

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    }


    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/user");
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        loadShiroFilterChain(shiroFilterFactoryBean);
        return shiroFilterFactoryBean;
    }
}
项目结构:


如图,实体和后台管理系统,没有在一个工程下。

然后报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializerPostProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.beans.factory.BeanFactory org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.beanFactory; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroFilter' defined in com.vanxd.admin.start.Start: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.apache.shiro.web.mgt.DefaultWebSecurityManager]: Error creating bean with name 'securityManager' defined in com.vanxd.admin.start.Start: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.vanxd.admin.shiro.realm.SysUserRealm]: Error creating bean with name 'myShiroRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.admin.service.user.SysUserService com.vanxd.admin.shiro.realm.SysUserRealm.sysUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.data.repository.SysUserRepository com.vanxd.admin.service.user.SysUserService.sysUserRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.vanxd.data.entity.user.SysUser; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myShiroRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.admin.service.user.SysUserService com.vanxd.admin.shiro.realm.SysUserRealm.sysUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.data.repository.SysUserRepository com.vanxd.admin.service.user.SysUserService.sysUserRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.vanxd.data.entity.user.SysUser; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManager' defined in com.vanxd.admin.start.Start: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.vanxd.admin.shiro.realm.SysUserRealm]: Error creating bean with name 'myShiroRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.admin.service.user.SysUserService com.vanxd.admin.shiro.realm.SysUserRealm.sysUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.data.repository.SysUserRepository com.vanxd.admin.service.user.SysUserService.sysUserRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.vanxd.data.entity.user.SysUser; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myShiroRealm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.admin.service.user.SysUserService com.vanxd.admin.shiro.realm.SysUserRealm.sysUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.vanxd.data.repository.SysUserRepository com.vanxd.admin.service.user.SysUserService.sysUserRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.vanxd.data.entity.user.SysUser

应该就是没有扫描到注解的问题。

加载中
返回顶部
顶部