Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

XML

Assign bean's property a Constant value in Spring config file

1
2
3
4
5
6
<property name="map">
            <map>
                <entry key="a"><util:constant static-field="util.ProjectConstants.A"/></entry>
                <entry key="r"><util:constant static-field="util.ProjectConstants.R"/></entry>                
            </map>
        </property>
https://snipt.net/embed/54750497770aa2dddcf11ce68eaa4281/
https://snipt.net/raw/54750497770aa2dddcf11ce68eaa4281/
54750497770aa2dddcf11ce68eaa4281
xml
XML
6
2016-12-02T13:50:38
True
False
False
/api/public/snipt/35401/
assign-beans-property-a-constant-value-in-spring-config-file
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;map&quot;</span><span class="nt">&gt;</span> </span><span id="L-2"><a name="L-2"></a> <span class="nt">&lt;map&gt;</span> </span><span id="L-3"><a name="L-3"></a> <span class="nt">&lt;entry</span> <span class="na">key=</span><span class="s">&quot;a&quot;</span><span class="nt">&gt;&lt;util:constant</span> <span class="na">static-field=</span><span class="s">&quot;util.ProjectConstants.A&quot;</span><span class="nt">/&gt;&lt;/entry&gt;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nt">&lt;entry</span> <span class="na">key=</span><span class="s">&quot;r&quot;</span><span class="nt">&gt;&lt;util:constant</span> <span class="na">static-field=</span><span class="s">&quot;util.ProjectConstants.R&quot;</span><span class="nt">/&gt;&lt;/entry&gt;</span> </span><span id="L-5"><a name="L-5"></a> <span class="nt">&lt;/map&gt;</span> </span><span id="L-6"><a name="L-6"></a> <span class="nt">&lt;/property&gt;</span> </span></pre></div> </td></tr></table>
java, spring

Java

Assign bean's property an Enum value in Spring config file

1
2
3
4
5
6
7
8
public enum MyEnumType {
    TYPE1,
    TYPE2
}

<bean name="someName" class="my.pkg.classes">
   <property name="type" value="TYPE1" />
</bean>
https://snipt.net/embed/18974b157a70a24a76c38ecc9e087c09/
https://snipt.net/raw/18974b157a70a24a76c38ecc9e087c09/
18974b157a70a24a76c38ecc9e087c09
java
Java
8
2016-12-02T09:18:39
True
False
False
/api/public/snipt/32755/
assign-beans-property-an-enum-value-in-spring-config-file
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">public</span> <span class="kd">enum</span> <span class="n">MyEnumType</span> <span class="o">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">TYPE1</span><span class="o">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">TYPE2</span> </span><span id="L-4"><a name="L-4"></a><span class="o">}</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="o">&lt;</span><span class="n">bean</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;someName&quot;</span> <span class="n">class</span><span class="o">=</span><span class="s">&quot;my.pkg.classes&quot;</span><span class="o">&gt;</span> </span><span id="L-7"><a name="L-7"></a> <span class="o">&lt;</span><span class="n">property</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;type&quot;</span> <span class="n">value</span><span class="o">=</span><span class="s">&quot;TYPE1&quot;</span> <span class="o">/&gt;</span> </span><span id="L-8"><a name="L-8"></a><span class="o">&lt;/</span><span class="n">bean</span><span class="o">&gt;</span> </span></pre></div> </td></tr></table>
spring

Java

How to obtain application context from web layer

1
2
3
4
5
6
7
// test the spring framework
ServletContext servletContext =this.getServletContext();

WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext(servletContext);

User user = (User)wac.getBean("user");
https://snipt.net/embed/040b2dcfe3faed94b03d58d2f0bc6e33/
https://snipt.net/raw/040b2dcfe3faed94b03d58d2f0bc6e33/
040b2dcfe3faed94b03d58d2f0bc6e33
java
Java
7
2016-12-02T09:18:50
True
False
False
/api/public/snipt/30047/
how-to-obtain-application-context-from-web-layer
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// test the spring framework</span> </span><span id="L-2"><a name="L-2"></a><span class="n">ServletContext</span> <span class="n">servletContext</span> <span class="o">=</span><span class="k">this</span><span class="o">.</span><span class="na">getServletContext</span><span class="o">();</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="n">WebApplicationContext</span> <span class="n">wac</span> <span class="o">=</span> <span class="n">WebApplicationContextUtils</span><span class="o">.</span> </span><span id="L-5"><a name="L-5"></a><span class="nf">getRequiredWebApplicationContext</span><span class="o">(</span><span class="n">servletContext</span><span class="o">);</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="n">User</span> <span class="n">user</span> <span class="o">=</span> <span class="o">(</span><span class="n">User</span><span class="o">)</span><span class="n">wac</span><span class="o">.</span><span class="na">getBean</span><span class="o">(</span><span class="s">&quot;user&quot;</span><span class="o">);</span> </span></pre></div> </td></tr></table>
java, spring

Text only

Spring @Transactional

1
2
3
The scope of @Transactional is per method invocation, as @Transactional is implemented through Spring AOP. The transactional state is kept in ThreadLocal variables which are initialized when the outermost @Transactional method is entered and cleared with commit or rollback when it is left. This whole abstraction works on Java method level, and hence does not require or profit from a web container.

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.
https://snipt.net/embed/11dfec5b972e104fcb56816b7bc7f49b/
https://snipt.net/raw/11dfec5b972e104fcb56816b7bc7f49b/
11dfec5b972e104fcb56816b7bc7f49b
text
Text only
3
2016-12-02T13:50:27
True
False
False
/api/public/snipt/30046/
spring-transactional
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>The scope of @Transactional is per method invocation, as @Transactional is implemented through Spring AOP. The transactional state is kept in ThreadLocal variables which are initialized when the outermost @Transactional method is entered and cleared with commit or rollback when it is left. This whole abstraction works on Java method level, and hence does not require or profit from a web container. </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a>In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. </span></pre></div> </td></tr></table>
spring
Copyrighted, illegal, or inappropriate content? Email [email protected].