我正在尝试运行以下代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.uprs.config.AppConfig;
import org.uprs.model.POC;
import org.uprs.repository.POCRepository;
public class test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
POC person = new POC();
person.setID(2l);
person.setName("Rohit");
person.setOwner("Mishra");
person.setEngagement("Pune");
context.getBean(POCRepository.class).save(person);
}
}
我的配置类是:
AppConfig.class
package org.uprs.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@ComponentScan( basePackages = "org.uprs.services")
@Import({RepositoryConfig.class})
public class AppConfig{
}
RepositoryConfig.class
package org.uprs.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
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.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@Configuration
@EnableJpaRepositories(basePackages = {"org.uprs.repository"})
public class RepositoryConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sakila");
dataSource.setUsername("root");
dataSource.setPassword("admin");
return dataSource;
}
@Bean
public Map<String, Object> jpaPropertyMap(){
Map<String, Object> jpaProperties = new HashMap<>();
jpaProperties.put("javax.persistence.validation.mode", "none");
jpaProperties.put("hibernate.event.merge.entity_copy_observer", "allow");
return jpaProperties;
}
/**
* Configuration object required by Hibernate
* @return EntityManagerFactoryBean
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("org.uprs.model");
emf.setJpaVendorAdapter(jpaVendorAdapter());
emf.setJpaPropertyMap(jpaPropertyMap());
return emf;
}
/**
* Configuration object required by Hibernate
* @return JpaVendorAdapter
*/
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
return hibernateJpaVendorAdapter;
}
/**
* Configuration object required by Hibernate
* @return JpaTransationManager
*/
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager txnMgr = new JpaTransactionManager();
txnMgr.setEntityManagerFactory(entityManagerFactory().getObject());
return txnMgr;
}
}
我的模型课是:POC。班
package org.uprs.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="contacts")
public class POC {
@Id
@Column(name="ID")
Long ID;
@Column(name="Name")
String Name;
@Column(name="Owner")
String Owner;
@Column(name="Engagement")
String engagement;
public Long getID() {
return ID;
}
public void setID(Long ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getOwner() {
return Owner;
}
public void setOwner(String Owner) {
this.Owner = Owner;
}
public String getEngagement() {
return engagement;
}
public void setEngagement(String engagement) {
this.engagement = engagement;
}
}
我的存储库类是:POCRepository类
package org.uprs.repository;
import org.springframework.data.repository.CrudRepository;
import org.uprs.model.POC;
public interface POCRepository extends CrudRepository<POC, Long> {
}
我的服务等级是:
package org.uprs.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.uprs.model.POC;
import org.uprs.repository.POCRepository;
@RestController
@RequestMapping("/poc")
public class POCService {
@Autowired
POCRepository pocRepository;
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<POC> getPOCDetails(){
return (List<POC>) pocRepository.findAll();
}
}
我正在运行main类,出现以下错误。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at test.main(test.java:9)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 13 more
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:53)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:456)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$23112fed.CGLIB$defaultServletHandlerMapping$39(<generated>)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$23112fed$$FastClassBySpringCGLIB$$d0aceb5c.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$23112fed.defaultServletHandlerMapping(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 14 more
我的网络。xml详细信息如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>UPRS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.uprs.config.AppConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
请帮帮我
在使用main类时,请删除@EnableWebMVC并运行。它会很好用的。
在测试环境中,请使用@WebAppConfiguration而不是@EnableWebMvc。
我是春靴新来的。我试图用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
我有h2数据库的工作应用程序。db没有连接问题,因为我可以从那里获得一些值,但是当我尝试创建EntityManagerFactory时:然后我在日志中看到: 只有在尝试创建上面提到的时才会出现问题。当我不尝试它时,我可以去h2 web控制台,看到flyway正确地创建了db,进行了插入,并且我可以进行选择/插入等等。 @编辑添加pom.xml
我是一个初学者,当我试图使用JPG创建标签时遇到了一些问题。 显示线程“main”java.lang.NullPointerException中的 异常 在Pane.myImageIcon.(myImageIcon.java:11) 在pane.myimageicon.main(myimageicon.java:21)
我有与h2数据库的工作应用程序。但是,当我尝试创建EntityManagerFactory:时,我可以从那里获得一些值,因此与db没有连接问题: @编辑添加pom.xml
我正在使用带有Boot的Spring Batch并使用XML配置来配置批处理作业。但是批处理作业是使用Tivoli调度的。同样,我希望通过使用批处理启动器进行配置来在本地运行。 我们怎样才能满足依赖性? 当我配置JobLauncher时 Database aseConfig.java XML配置:
我在线程“main”java中遇到异常。错误:未解析编译。 我做错了什么? 线程“main”中出现异常: java.lang.错误:未解决的编译问题: 对于类型在