person.java
package com.test.business;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "person")
public class Person {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public Person(){
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
//getters and setters
}
PersonRepository.java
package com.test.repository;
import com.test.business.Person;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, Long> {
}
PersonService.java
package com.test.service;
import com.test.business.Person;
import com.test.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonService{
@Autowired
private PersonRepository personRepository;
public void addNewPerson(){
personRepository.save(new Person(2, "Test2"));
}
}
PersonController.java
package com.test.controller;
import com.test.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@GetMapping(value="/")
@ResponseBody
public String printWelcome() {
personService.addNewPerson();
return "home";
}
MyWebInitializer.java
package com.test.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
WebConfig.java
package com.test.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@Configuration
@ComponentScan({ "com.test.controller" })
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
rootconfig.java
package com.test.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableJpaRepositories( basePackages = {"com.test.repository"})
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.test.service", "com.test.repository", "com.test.controller", "com.test.business"})
public class RootConfig {
@Autowired
private DataSource dataSource;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
dataSource.setUsername("postgres");
dataSource.setPassword("postgres");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(getClass().getPackage().getName());
factory.setDataSource(dataSource());
factory.setJpaProperties(jpaProperties());
return factory;
}
private Properties jpaProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.show_sql", "true");
return properties;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
}
Build.Gradle
plugins {
id 'java'
}
apply plugin: 'war'
group 'testApp'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.tomcat:tomcat-catalina:9.0.10'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.0.3.RELEASE'
compile 'org.hibernate:hibernate-core:5.3.2.Final'
compile 'org.springframework.data:spring-data-jpa:2.0.9.RELEASE'
compile 'org.postgresql:postgresql:42.2.3'
compile 'org.springframework:spring-webmvc:5.0.7.RELEASE'
compile 'org.hibernate:hibernate-entitymanager:5.3.3.Final'
compile 'javax.xml.bind:jaxb-api:2.3.0'
}
构建此项目时没有错误,但当我尝试运行此应用程序时,控制台显示错误:
错误org.springframework.web.Context.contextloader-上下文初始化失败org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“Person Service”的bean时出错:通过字段“Person Repository”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“Person Repository”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:不是托管类型:class com.test.business.Person
(...)
原因:org.springframework.beans.factory.BeanCreationException:创建名为“Person Repository”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:不是托管类型:class com.test.business.Person
(...)
原因:java.lang.IllegalArgumentException:不是托管类型:class com.test.business.Person
这是为什么?为什么不能创建personService bean?我在Person.java中有@Entity,包括autowired,RootConfig包含@ComponentScan
@ComponentScan(basePackages = {"com.test.service", "com.test.repository", "com.test.controller", "com.test.business"})
你能给我一些建议来解决我的问题吗?
在上下文初始化时,spring无法将您的实体类标识为有效实体。要使它们有效,请尝试下列操作之一:
Factory.SetPackageStoscan(getClass().GetPackage().GetName());
更改为Factory.SetPackageStoscan(“com.test.business”);
。否则com.test.config
包将用于实体扫描。factory.setPackageStoscan(getClass().getPackage().getName());
并将@EntityScan(“com.test.business”)
放在RootConfig.java
另请参阅@EntityScan和@ComponentScan之间的区别
我试图编译一个非常简单的程序,将包含3个用户的简单表保存到http://localhost/phpmyadmin,以清空名为,users ' '的数据库,但它仍然显示异常,您可以看到。 1个异常org.springframework.beans.factory。BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure
当我启动Weblogic时(使用jar:hibernate-core-4.3.6.final.jar和hibernate-jpa-2.1-api-1.0.0.final.jar),遇到以下错误信息: 无法自动连接字段:private org.hibernate.sessionFactory com.nscorp.lars.shopleveling.core.dao.impl.Dataloaddao
我在SpringBoot应用程序中创建HighHendRestClient bean时遇到一个错误。我已经做了一个测试'app',在那里我检查了我可以实例化我想要的对象,然后进行我想要的调用,我现在正在做一个新的应用程序的婴儿步骤。 就我所能看到的(我还没有用它做太多...) 当我添加它时(最初我传入了RestClient bean,但现在我临时创建了一个本地对象,以便更清晰) 我得到这个java
我使用的是Spring 3.1.4 服务实现 DAO实现 web.xml
严重:上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建ServletContext资源[/web-inf/mvc-dispatcher-servlet.xml]中定义的名为“org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerM
我是春靴新来的。我试图用hibernate连接MySql db,但当我命令 mvn Spring-Boot:Run 我的pom.xml在这里: hibernate.dialog:org.hibernate.dialt.mysql5dialog hibernate.show_sql:true hibernate.hbm2ddl.auto:update entitymanager.packageSto