2017-04-25 8 views
0

最近、インタビューで、SpringアプリケーションやHibernateアプリケーションをXML構成なしで使用することが可能かどうか尋ねられました。 それで最高の答えが何であるか説明してください。XMLアプリケーションを持たないSpringアプリケーションとHibernateアプリケーション

+2

はいコンフィギュレーションの2種類があり、すなわちXMLベースのベース&注釈の設定 –

+0

私は「それがXML設定せずに春のアプリケーションまたは休止状態のアプリケーションを持つことが可能である、」聞いていますのよ..? –

+1

これは私のコメントが示唆していることです。はい –

答えて

0

はいすることができます、例えば: - >アカウント

@Entity 
public class Account { 
    @Id 
    @GeneratedValue 
    private Long id; 
    @Temporal(TemporalType.DATE) 
    private Data data; 
    private Long accountNumber; 
    private String owner; 
    private double balance; 

    public Long getAccountNumber() { 
     return accountNumber; 
    } 

    public void setAccountNumber(Long accountNumber) { 
     this.accountNumber = accountNumber; 
    } 

    public String getOwner() { 
     return owner; 
    } 

    public void setOwner(String owner) { 
     this.owner = owner; 
    } 

    public double getBalance() { 
     return balance; 
    } 

    public void setBalance(double balance) { 
     this.balance = balance; 
    } 

    @Override 
    public String toString() { 
     return "Account{" + 
       "accountNumber=" + accountNumber + 
       ", owner='" + owner + '\'' + 
       ", balance=" + balance + 
       '}'; 
    } 
} 

ダオ:

@Repository 
public class AccountDao { 

    @PersistenceContext 
    EntityManager entityManager; 

    @Transactional 
    public void save(Account account) { 
     entityManager.persist(account); 
    } 

    protected Account getByKey(Long key) { 
     return (Account) entityManager.find(Account.class, key); 
    } 
} 

設定 - >

@Configuration 
@EnableTransactionManagement 
@ComponentScan("your scan package path") 
public class AppConfig { 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); 
     driverManagerDataSource.setUsername("root"); 
     driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/your_data_base_name"); 
     driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
     driverManagerDataSource.setPassword("password"); 
     return driverManagerDataSource; 
    } 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
     localContainerEntityManagerFactoryBean.setDataSource(dataSource()); 
     localContainerEntityManagerFactoryBean.setJpaVendorAdapter(getHibernateJpaVendorAdapter()); 
     localContainerEntityManagerFactoryBean.setJpaProperties(additionalProperties()); 
     localContainerEntityManagerFactoryBean.setPackagesToScan("entity_package_name"); 
     return localContainerEntityManagerFactoryBean; 
    } 

    @Bean 
    public JpaVendorAdapter getHibernateJpaVendorAdapter() { 
     HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); 
     hibernateJpaVendorAdapter.setShowSql(true); 
     hibernateJpaVendorAdapter.setGenerateDdl(true); 
     return hibernateJpaVendorAdapter; 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 
     return new JpaTransactionManager(entityManagerFactory().getObject()); 
    } 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    Properties additionalProperties() { 
     Properties properties = new Properties(); 
     properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); 
     properties.put("hibernate.hbm2ddl.auto", "create"); 
     properties.put("hibernate_show_sql", true); 
     return properties; 
    } 
} 

Testクラス:

public class Test { 

    public static void main(String[] args) { 
     ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); 
     Account account = new Account(); 
     account.setAccountNumber(123L); 
     account.setBalance(100056.5); 

     AccountService accountService = applicationContext.getBean(AccountService.class); 
     accountService.save(account); 
    } 
} 
関連する問題