Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建bean时出错_Java_Spring_Hibernate - Fatal编程技术网

Java 创建bean时出错

Java 创建bean时出错,java,spring,hibernate,Java,Spring,Hibernate,大家晚上好 我在做一些愚蠢的事情,但似乎无法让Spring认出我的豆子。我不确定我做错了什么。我得到的印象是,我没有设置正确的豆子,但我不能肯定。提前谢谢 主要类别: @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); }

大家晚上好

我在做一些愚蠢的事情,但似乎无法让Spring认出我的豆子。我不确定我做错了什么。我得到的印象是,我没有设置正确的豆子,但我不能肯定。提前谢谢

主要类别:

@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
   }
}
我的配置:

@Configuration
public class HibernateConfig {

    @Value("${db.driver}")
    private String DB_DRIVER;

    @Value("${db.password}")
    private String DB_PASSWORD;

    @Value("${db.url}")
    private String DB_URL;

    @Value("${db.username}")
    private String DB_USERNAME;

    @Value("${hibernate.dialect}")
    private String HIBERNATE_DIALECT;

    @Value("${hibernate.show_sql}")
    private String HIBERNATE_SHOW_SQL;

    @Value("${hibernate.hbm2ddl.auto}")
    private String HIBERNATE_HBM2DDL_AUTO;

    @Value("${entitymanager.packagesToScan}")
    private String ENTITYMANAGER_PACKAGES_TO_SCAN;


    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClass(DB_DRIVER);
        dataSource.setJdbcUrl(DB_URL);
        dataSource.setUser(DB_USERNAME);
        dataSource.setPassword(DB_PASSWORD);
        return dataSource();
    }


    @Bean
    public LocalSessionFactoryBean sessionFactory(){
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource((javax.sql.DataSource) dataSource());
        sessionFactory.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
        hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
        hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
        sessionFactory.setHibernateProperties(hibernateProperties);
        return sessionFactory();
    }
}
My application.properties文件:

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false
db.username=springstudent
db.password=springstudent

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
entitymanager.packagesToScan=springtutorial
我的建议:

@Repository
public class CustomerDaoImpl implements CustomerDao{

    @Autowired
    private SessionFactory sessionFactory;



    @Override
    public List<Customer> getCustomers() {


        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        Query<Customer> theQuery = session.createQuery("FROM CUSTOMER", Customer.class);

        List<Customer> customers = theQuery.getResultList();

        session.getTransaction().commit();

        return customers;
    }
}

您需要在application.properties文件中提供spring.datasource.url。这对我有用。请参见

您需要在application.properties文件中提供spring.datasource.url。这对我有用。请参见

Spring boot的整个理念是减少配置,改用常规。你正在尝试使用SpringBoot,还试图配置数据源并自己休眠,这是没有用的。只需将所需的启动器添加到pom或gradle构建文件中。只添加mysql驱动程序和
org.springframework.boot:springbootstarterjpa
进入您的类路径

将以下条目添加到application.properties/yml文件中

spring.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false
spring.datasource.username=springstudent
spring.datasource.password=springstudent
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
SpringBoot自动配置会选择数据源属性并为您创建一个数据源。此外,JPA配置将通过hibernate完成,EntityManager/SessionFactory将为您创建,您可以在需要时自动连线。 您可以放弃@Configuration类,直接在代码中自动连接datasource/sessionfactory

确保您的实体位于使用@EnableAutoConfiguration类的子包中,以便它可以拾取它们。还考虑使用@SpringBootApplication作为@EnableAutoConfiguration和@ComponentScan的方便的注释替代方案

如果你真的想自己配置数据源,那么就排除Spring引导自动配置

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

Spring boot的整个理念是减少配置,而是使用约定。你正在尝试使用SpringBoot,还试图配置数据源并自己休眠,这是没有用的。只需将所需的启动器添加到pom或gradle构建文件中。只添加mysql驱动程序和
org.springframework.boot:springbootstarterjpa
进入您的类路径

将以下条目添加到application.properties/yml文件中

spring.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false
spring.datasource.username=springstudent
spring.datasource.password=springstudent
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
SpringBoot自动配置会选择数据源属性并为您创建一个数据源。此外,JPA配置将通过hibernate完成,EntityManager/SessionFactory将为您创建,您可以在需要时自动连线。 您可以放弃@Configuration类,直接在代码中自动连接datasource/sessionfactory

确保您的实体位于使用@EnableAutoConfiguration类的子包中,以便它可以拾取它们。还考虑使用@SpringBootApplication作为@EnableAutoConfiguration和@ComponentScan的方便的注释替代方案

如果你真的想自己配置数据源,那么就排除Spring引导自动配置

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

嗨,欢迎来到Stack Overflow。试着解释一下你提出的解决方案。共享链接没关系,但您应该添加一点说明;)嗨,欢迎来到Stack Overflow。试着解释一下你提出的解决方案。共享链接没关系,但您应该添加一点说明;)