Using EntityManagerFactory and ApplicationContext

I want to make sure that since I use @PersistenceContext, I do not need to close any connections to avoid leaks and any open open connections and poor performance. So my applicationContext.xml is as follows (where I define the entitymanager factory, etc.)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

<context:component-scan base-package="com.companyname.*" />

<tx:annotation-driven/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:com/urbanbuz/controller/persistence.xml" />
    <property name="persistenceUnitName" value="userPersistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="jpaDialect" ref="jpaDialect" />
</bean>

<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="MYSQL" />
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    <property name="showSql" value="true"/>
</bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect" ref="jpaDialect" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ub" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

My xml persistence is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="userPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.urbanbuz.model.User</class>
    <class>com.urbanbuz.model.Account</class>
    <class>com.urbanbuz.model.AccountDetails</class>
    <class>com.urbanbuz.model.Posting</class>
    <class>com.urbanbuz.model.Journal</class>
</persistence-unit>

Now for each of these models I have a DAO and Service class, as an example I provide one:

@Repository("accountDao")
@Transactional(propagation = Propagation.REQUIRED)
public class AccountDAO {
@PersistenceContext
private EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

// method inserts account into database
public void insert(Account account) {
    entityManager.persist(account);
}
}

@Service
public class AccountService {

private AccountDAO accountDAO;

public AccountDAO getAccountDao() {
    return accountDAO;
}

@Autowired
public void setAccountDao(AccountDAO accountDAO) {
    this.accountDAO = accountDAO;
}

public void addAccount(Account account) {
    getAccountDao().insert(account);
}
}

, , : ApplicationContext context = new ClassPamlApplicationContext ( "applicationContext.xml" ), EntityService entityService = (EntityService) context.getBean( "entityService" ) , , . - ?

: App.Java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class App {
    public static void main(String[] args) {
     // here i just initialize an instance of the a component I have
     SignupComponent sc = new SignupComponent();
     // some code
     sc.signUp();
    } 
}

autwire :

public class SignupComponent {
    @Autowired
    EntityService entityService;

    //using it as follows for example: entityService.getEntity(entity_id);
}
+4
2

: :

<tx:annotation-driven/>

<tx:annotation-driven transaction-manager="transactionManager" />

, transactionManager.

, Spring Hibernate .

, :

  • Spring.

  • @Autowired

    :

    EntityService entityService = (EntityService) context.getBean("entityService");
    

    :

    @Autowired
    private EntityService entityService;
    
    public void callService() {
         entityService.call();
    }
    

Update

, , , :

  • , :

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/applicationContext.xml" })
    
  • Boostrap your context:

    public static void main(String args[]) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
        context.registerShutdownHook();
        EntityService entityService = (EntityService) context.getBean("entityService");
    }
    
+1

, spring. , , , spring ...

. ApplicationContext , , .. , , spring XML- .

applicationContext.xml


    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
    <mvc:annotation-driven/>

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config/>

    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package -->
    <context:component-scan base-package="com.farah"/>

    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <!-- Imports datasource configuration -->
    <import resource="spring-data.xml"/>  

</beans>

, Spring-Data-JPA, spring, , .

spring -data.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:component-scan base-package="com.farah.repository.impl"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

        </property>

        <property name="jpaProperties">
            <map>
                <entry key="hibernate.hbm2ddl.auto" value="create-drop"/>
                <entry key="hibernate.show_sql" value="true"/>
            </map>
        </property>

        <property name="packagesToScan" value="com.farah.domain"/>
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ub" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
</beans>

, spring Data JPA , . JpaRepository, CRUD. , , .

AccountRepository


/**
 * Spring Data JPA repository for the Account entity.
 */
public interface AccountRepository extends JpaRepository<Account, Long> {

}

JpaRepository

.


/**
* An Account.
*/
@Entity
@Table( name = "T_ACCOUNT" )
public class Account implements Serializable {

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO ) private Long id;
    @Column( name = "name" ) private String name;
    /**
     * Getters, Setters... 
     */
     ...
}

@Inject .

ServiceFacade

AccountService


@Transactional
@Service
public class AccountService {

    @Autowired
    private AccountRepository repository;

    public Boolean create( Account Account ) {
        ...
    }

    public Boolean update( Account Account ) {
       ...
    }

    public Boolean delete( Account Account ) {
        ...
    }
}
0

Source: https://habr.com/ru/post/1569201/


All Articles