UnsatisfiedDependencyException:使用name创建bean时出错

时间:2017-06-07 23:43:21

标签: spring spring-mvc

我正在尝试在Spring上创建数据库服务器,并查看了很多类似的问题,但没有人回答它们无法帮助我。我无法解决这个错误:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'groupController'的bean时出错:通过字段'gs'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'groupServiceImpl'的bean时出错:通过字段'gr'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'groupRepository'的bean时出错:设置bean时无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)#1a3c15e0' property'entalManager';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#1a3c15e0'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'entityManagerFactory'的bean可用

所以这是我的课程:

GroupController.java

 @RestController
public class GroupController {

    @Autowired
    private GroupService gs;


    @RequestMapping(value = "/groups", method = RequestMethod.GET)
    @ResponseBody
    public List<Group> getAllGroups(){
       // return mocklist();
        return gs.getAll();
    }

    private List<Group> mocklist() {
        Group g= new Group();
        g.setId(1);
        g.setGroupName("333");
        g.setTutorName("shcefsa");
        List<Group> list = new ArrayList<Group>();
        list.add(g);
        g.setId(2);
        g.setGroupName("333");
        g.setTutorName("shc321efsa");
        list.add(g);
        return list;
    }


    @RequestMapping(value = "/groups/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Group getGroupById(@PathVariable("id") int groupId){
        return gs.getById(groupId);
    }


    @RequestMapping(value = "/groups", method = RequestMethod.POST)
    @ResponseBody
    public Group addGroup(@RequestBody Group group){
        return gs.add(group);
    }


    @RequestMapping(value = "/groups", method = RequestMethod.POST)
    @ResponseBody
    public Group updateGroup(@RequestBody Group group){
        return gs.add(group);
    }

    @RequestMapping(value = "/groups/{id}", method = RequestMethod.POST)
    @ResponseBody
    public void deleteGroup(@PathVariable("id") int groupId){
        gs.deleteById(groupId);
    }


}

GroupRepository.java

@Repository
public interface GroupRepository extends JpaRepository<Group, Integer> {


}

GroupService.java

@Service
public interface GroupService {

    List<Group> getAll();
    Group getById(int id);
    Group add(Group group);
    void deleteById(int id);

}

GroupServiceImpl.java `

@Service
public class GroupServiceImpl implements GroupService {
@Autowired
private GroupRepository gr;

public List<Group> getAll() {
    return gr.findAll();
}

public Group getById(int id) {
    return gr.findOne(id);
}

public Group add(Group group) {
    return gr.save(group);
}

public void deleteById(int id) {
    gr.delete(id);
}

看看其他答案,似乎一切都很好,但显然存在错误。希望你能帮助我

更新

Databaseconfig.java

    @Configuration
@EnableJpaRepositories("com.homestudio.tutor.server.repository")
@EnableTransactionManagement
@PropertySource("classpath:db.properties")
@ComponentScan("com.homestudio.tutor.server")
public class DatabaseConfig {


    @Resource
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(env.getRequiredProperty("db.entity.package"));
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(getHibernateProperties());

        return em;
    }



    @Bean
    public PlatformTransactionManager platformTransactionManager(){
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(entityManagerFactoryBean().getObject());

        return manager;

    }


    @Bean
    public DataSource dataSource(){
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(env.getRequiredProperty("db.url"));
        ds.setDriverClassName(env.getRequiredProperty("db.driver"));
        ds.setUsername(env.getRequiredProperty("db.username"));
        ds.setPassword(env.getRequiredProperty("db.password"));

        ds.setInitialSize(Integer.valueOf(env.getRequiredProperty("db.initialSize")));
        ds.setMinIdle(Integer.valueOf(env.getRequiredProperty("db.minIdle")));
        ds.setMaxIdle(Integer.valueOf(env.getRequiredProperty("db.maxIdle")));
        ds.setTimeBetweenEvictionRunsMillis(Long.valueOf(env.getRequiredProperty("db.timeBetweenEvictionRunsMilles")));
        ds.setMinEvictableIdleTimeMillis(Long.valueOf(env.getRequiredProperty("db.minEvictableIdleTimeMilles")));
        ds.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty("db.testOnBorrow")));
        ds.setValidationQuery(env.getRequiredProperty("db.validationQuery"));

        return ds;

    }

    public Properties getHibernateProperties() {

        try {
        Properties properties = new Properties();
        InputStream is = getClass().getClassLoader().getResourceAsStream("hibernate.properties");

        properties.load(is);

            return properties;
        } catch (IOException e) {
            throw new IllegalArgumentException("Cannot find file \"hibernate.properties\" in classpath");
        }



    }
}

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan("com.homestudio.tutor.server")
public class WebConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new ObjectMapper());
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));


        converters.add(converter);

    }
}

0 个答案:

没有答案