【问题标题】:SpringBoot - Websphere - Java Config - web.xml - resource-refSpringBoot-Websphere-Java Config-web.xml-resource-ref
【发布时间】:2019-01-26 04:58:10
【问题描述】:

我有一个 Web 应用程序,其中我在 web.xml 中定义了 3 个 jndi 资源 1个用于数据库,2个用于动态缓存

如何在 Spring boot 中将其转换为 Java 配置。

以下是应用程序中的示例资源引用配置

<resource-ref>
    <description>Resource reference for database</description>
    <res-ref-name>jdbc/dbname</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

<resource-ref id="cache1">
    <description>cache1 description</description>
    <res-ref-name>cache/cache1</res-ref-name>
    <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

<resource-ref id="cache2">
    <description>cache2 description</description>
    <res-ref-name>cache/cache2</res-ref-name>
    <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

谢谢

【问题讨论】:

  • 我是 Spring 用户,但不是 Spring Boot。你使用什么样的 Spring 配置?是 XML 文件,还是只是基于注释的配置?
  • 目前它是一个带有 web.xml 的 spring mvc 应用程序。我想将应用程序移动到 Spring boot 并将配置转换为基于 java 的注释。
  • 您是否尝试过使用@Resource(name="jdbc/dbname")
  • 你必须把它们放在一个ibm-web-bnd.xml 文件中。
  • Spring Boot 和嵌入式服务器仅支持 Tomcat、Jetty 和 Undertow。你打算从 WebSphere 迁移到以上任何一个吗?

标签: spring-boot websphere


【解决方案1】:
@Configuration
@EnableTransactionManagement
@ComponentScan("org.example")
@EnableJpaRepositories(basePackages = "org.yours.persistence.dao")
public class PersistenceJNDIConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
      throws NamingException {
        LocalContainerEntityManagerFactoryBean em 
          = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());

        // rest of entity manager configuration
        return em;
    }

    @Bean
    public DataSource dataSource() throws NamingException {
        return (DataSource) new JndiTemplate().lookup("jdbc/dbname");
    }

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

    // rest of persistence configuration
}

我们将使用带有 @Entity 注释的简单模型,并带有生成的 id 和名称:

@Entity
public class Foo {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME")
    private String name;

    // default getters and setters
}

让我们定义一个简单的存储库:

@Repository
public class FooDao {

    @PersistenceContext
    private EntityManager entityManager;

    public List<Foo> findAll() {
        return entityManager
          .createQuery("from " + Foo.class.getName()).getResultList();
    }
}

最后,让我们创建一个简单的服务:

@Service
@Transactional
public class FooService {

    @Autowired
    private FooDao dao;

    public List<Foo> findAll() {
        return dao.findAll();
    }
}

有了这个,您就拥有了在 Spring 应用程序中使用 JNDI 数据源所需的一切。

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2019-07-28
    • 2014-02-27
    • 2011-03-17
    • 2017-09-21
    • 2010-12-25
    相关资源
    最近更新 更多