未注入Websphere Liberty 18.0.0.3 MySQL数据源对象
我的web应用程序未获取在server.xml中配置的数据源。我已经在文件夹未注入Websphere Liberty 18.0.0.3 MySQL数据源对象,mysql,websphere-liberty,Mysql,Websphere Liberty,我的web应用程序未获取在server.xml中配置的数据源。我已经在文件夹C:\wlp\usr\shared\resources\mysql server.xml <!-- Enable features --> <featureManager> <feature>cdi-1.2</feature> <feature>jaxrs-2.0</feature&
C:\wlp\usr\shared\resources\mysql
server.xml
<!-- Enable features -->
<featureManager>
<feature>cdi-1.2</feature>
<feature>jaxrs-2.0</feature>
<feature>jdbc-4.0</feature>
<feature>jndi-1.0</feature>
<feature>jpa-2.0</feature>
<feature>localConnector-1.0</feature>
<feature>servlet-3.1</feature>
</featureManager>
<!-- Declare the jar files for MySQL access through JDBC. -->
<library id="MySQLLib">
<fileset dir="${shared.resource.dir}/mysql" includes="mysql-connector-java-8.0.12.jar"/>
</library>
<!-- Declare the runtime database -->
<dataSource jndiName="AdminWeb/jdbc/AdminDS" transactional="false">
<jdbcDriver libraryRef="MySQLLib"/>
<properties databaseName="admin" password="****" portNumber="3306" serverName="localhost" user="root"/>
</dataSource>
调用webapp时,getconnection方法抛出
[错误]SRVE0777E:应用程序类'com.fist.tools.admin.dao.UserDAO.getAllUsers:25'引发异常
java.lang.NullPointerException
谁能帮我一下吗 数据源/服务器配置本身看起来不错@资源只能注入到web组件/ejb组件中。您注入的类是否符合该描述?我试图从JSP调用JAX-RS IBM Web服务来显示用户详细信息。Web服务调用数据库来获取用户记录。
com.first.tools.admin.dao.UserDAO
类是托管类吗?托管类包括CDIBeans、servlet、EJB和JAX-RS资源类。用户DAO是一个与数据库通信的普通Java类。通过如下@PersistenceContext(name=“AdminWeb/jdbc/AdminDS”)私有EntityManager EntityManager的持久性上下文注释检索实体管理器实例;我是否需要添加server.xml以外的任何其他配置?这意味着您尝试的使用超出了Java EE规范的范围。请尝试将您的@Resource
使用移动到Andy或我在前面的评论中指出的托管类类型之一。然后,您可以让它将注入的数据源实例传递给您的UserDAO。另外,您可以考虑将UsSero纯java对象转换为托管类(如CDI bean)是否合理。
@Resource(name = "AdminWeb/jdbc/AdminDS",lookup="AdminWeb/jdbc/AdminDS")
DataSource dataSource;
public UserEntity getAllUsers() {
UserEntity user = new UserEntity();
Connection connection = null;
try {
System.out.println("****************1");
connection = dataSource.getConnection();
System.out.println("2");