Websphere Liberty 18.0.0.3 MySQL数据源对象未注入

时间:2018-10-19 16:31:13

标签: mysql websphere-liberty

我的Web应用程序未获取在server.xml中配置的数据源。我在文件夹C:\wlp\usr\shared\resources\mysql下添加了sqlconnector jar(mysql-connector-java-8.0.12)

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>

DAO

@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");

调用webapp时,getconnection方法抛出

  

[错误] SRVE0777E:应用程序类'com.fist.tools.admin.dao.UserDAO.getAllUsers:25'引发异常。   java.lang.NullPointerException

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

dataSource /服务器配置本身看起来不错。 @Resource只能注入到Web组件/ ejb组件中。您要注入的课程符合该说明吗?