我在Spring中创建简单的上下文以使其处理hibernate的东西。您可以找到我的项目here。
错误:
/usr/lib/jvm/default/bin/java -Dmaven.home=/usr/share/intellijidea-ce/plugins/maven/lib/maven3 -Dclassworlds.conf=/usr/share/intellijidea-ce/plugins/maven/lib/maven3/bin/m2.conf -Didea.launcher.port=7536 -Didea.launcher.bin.path=/usr/share/intellijidea-ce/bin -Dfile.encoding=UTF-8 -classpath /usr/share/intellijidea-ce/plugins/maven/lib/maven3/boot/plexus-classworlds-2.4.jar:/usr/share/intellijidea-ce/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.1.1 clean compile exec:java -Dexec.mainClass=com.pizzaa.application.App
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ application ---
[INFO] Deleting /home/jjpikoov/IdeaProjects/pizzaa/application/target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ application ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/jjpikoov/IdeaProjects/pizzaa/application/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ application ---
[INFO] Compiling 11 source files to /home/jjpikoov/IdeaProjects/pizzaa/application/target/classes
[INFO]
[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ application ---
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.context.annotation.ScopedProxyMode org.springframework.context.annotation.ComponentScan.scopedProxy()
at java.lang.reflect.Method.getDefaultValue(Method.java:612)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:128)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
我在这里创建上下文:
package com.pizzaa.application;
import com.pizzaa.application.interfaces.AdminService;
import com.pizzaa.domain.admin.Admin;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by jjpikoov on 5/14/16.
*/
public class App {
public static void main(String [] args) {
// AbstractApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
AdminService adminService = (AdminService) context.getBean("adminService");
Admin a1 = new Admin();
a1.setLogin("jjpikoov");
a1.setPassword("jjpassw");
a1.setEmail("emailjj");
adminService.addAdmin(a1);
context.close();
}
}
配置:
package com.pizzaa.application;
import com.pizzaa.infrastructure.dbrepository.AdminRepositoryIM;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by jjpikoov on 5/31/16.
*/
@Configuration
@ComponentScan(basePackageClasses = {AdminRepositoryIM.class, AdminServiceIM.class, SpringConfiguration.class})
public class Config {
}
SpringConfig:
package com.pizzaa.application;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
/**
* Created by jjpikoov on 5/31/16.
*/
//@ComponentScan(basePackageClasses = {AdminRepositoryIM.class, AdminServiceIM.class})
@Configuration
@EnableTransactionManagement
public class SpringConfiguration {
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] {"com.pizzaa.domain"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/pizza");
dataSource.setUsername("jjpikoov");
dataSource.setPassword("pizza"); //bad practise!!!!
return dataSource;
}
private Properties hibernateProperties(){
Properties properties = new Properties();
properties.put("connection_pool_size", 1);
properties.put("hbm2ddl.auto", "create");
properties.put("show_sql", "true");
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sf){
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sf);
return txManager;
}
}
服务:
package com.pizzaa.application;
import com.pizzaa.application.interfaces.AdminService;
import com.pizzaa.domain.admin.Admin;
import com.pizzaa.domain.admin.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by jjpikoov on 5/4/16.
*/
@Service("adminService")
@Transactional
public class AdminServiceIM implements AdminService {
@Autowired
private AdminRepository adminRepository;
// public AdminServiceIM(){
// this.adminRepository = new AdminRepositoryIM();
// }
// public AdminServiceIM(AdminRepository adminRepository){
// this.adminRepository = adminRepository;
// }
// @Transactional
public List<Admin> getAllAdmins() {
return adminRepository.findAll();
}
// @Transactional
public void addAdmin(Admin a) {
this.adminRepository.insert(a);
}
// @Transactional
public boolean removeAdmin(int id) {
return this.adminRepository.delete(id);
}
// @Transactional
public Admin getAdmin(int id) {
return this.adminRepository.find(id);
}
}
DAO:
package com.pizzaa.infrastructure.dbrepository;
import com.pizzaa.domain.admin.Admin;
import com.pizzaa.domain.admin.repository.AdminRepository;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by jjpikoov on 5/30/16.
*/
@Repository
public class AdminRepositoryIM implements AdminRepository {
@Autowired
private SessionFactory sessionFactory;
public void insert(Admin x) {
sessionFactory.getCurrentSession().save(x);
}
public boolean delete(int id) {
Session s = sessionFactory.getCurrentSession();
Admin a = s.byId(Admin.class).load(id);
s.delete(a);
if (a == null)
return false;
return true;
}
public Admin find(int id) {
return sessionFactory.getCurrentSession().byId(Admin.class).load(id);
}
public List<Admin> findAll() {
return sessionFactory.getCurrentSession().createCriteria(Admin.class).list();
}
// public List<Admin> findAll() {
// return sessionFactory.getCurrentSession().createCriteria(Admin.class).list();
// }
}
域:
package com.pizzaa.domain.admin;
import javax.persistence.*;
/**
* Created by jjpikoov on 4/9/16.
*/
@Entity
@Table
public class Admin {
@Id
@GeneratedValue
@Column(nullable = false, unique = true)
private int id;
@Column(nullable = true)
private String login;
@Column(nullable = false)
private String password;
@Column(nullable = true)
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我几乎尝试了一切。你知道我做错了什么吗?提前谢谢。
答案 0 :(得分:0)
是版本问题。
从bean类中删除@Configuration
。希望它能解决你的问题。
资源链接: