Making CDI available in Apache Tomcat is tedious by writing and configuring pom.xml. So, this time, I will use Apache OpenWebBeans to make it easy to use CDI.
/Work destination/
├ Tomcat/
│ ├ apache-tomcat-9.0.22/
│ │ ├ bin/
│ │ │ ├ startup.bat
│ │ │ └ startup.sh
│ │ └ conf/
│ │ ├ Catalina/
│ │ │ └ localhost
│ │ │ └ Sample.xml
│ │ ├ context.xml
│ │ └ logging.properties
│ ├ current -> (/Work destination/Tomcat/apache-tomcat-9.0.22/)
│ ├ openwebbeans-distribution-2.0.9
│ │ ├ install_owb_tomcat7.bat
│ │ └ install_owb_tomcat7.sh
│ ├ apache-tomcat-9.0.22
│ └ openwebbeans-distribution-2.0.9-binary.zip
└ quita
└ cdi-sample
└ WebContent
└ WEB-INF
└ beanse.xml
Create a directory for this work. (Hereafter, work destination)
Create a Tomcat directory at your destination.
Download Apache Tomcat to the Tomcat directory and unzip it.
Create a current link in consideration of the version change.
#For Windows
mklink /j current apache-tomcat-9.0.22
#For linux
ln -s apache-tomcat-9.0.22 ./current
[Implemented for Windows] Change conf / logging.properties in Apache Tomcat to prevent the console from being garbled.
#Near line 51
#Change before
java.util.logging.ConsoleHandler.encoding = UTF-8
#After change
java.util.logging.ConsoleHandler.encoding = SJIS
Download Apache OpenWebBeans to the Tomcat directory and unzip it.
Run install_owb_tomcat7 in Apache OpenWebBeans
#For Windows
install_owb_tomcat7.bat ..\current
#For linux
install_owb_tomcat7.sh ../current
[Implemented for Windows] Add a tag in the "
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!--↓ Added tag-->
<Listener className="org.apache.webbeans.web.tomcat7.ContextLifecycleListener"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
</Context>
Clone the following Git to your destination
Create xml with the following contents in conf / Catalina / localhost in Apache Tomcat.
``` xml:Sample.xml
<Context path="/Sample" reloadable="true" docBase="/Work destination/quita/cdi-sample/WebContent">
</Context>
```
``` sh
#For Windows
startup.bat
#For linux
startup.sh
```
Now I've used Apache OpenWebBeans to enable Apache Tomcat to use CDI. In the case of Windows, it is a little annoying because it requires manual work, but I wondered if it would be easy to use CDI by executing bat / sh. The source used this time is created with eclipse, so if you can, you may want to play around with it. (When using with eclipse, change the Tomcat installation directory to / work / Tomcat / current from Window-> Settings-> Server-> Runtime environment-> Tomcat9 (Java11)-> Edit)
Please comment if you have any mistakes or problems.
Recommended Posts