<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Richard Hsu on Medium]]></title>
        <description><![CDATA[Stories by Richard Hsu on Medium]]></description>
        <link>https://medium.com/@litehsu?source=rss-245aa36ea0d1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*oBKigHwnVW7orTNX.</url>
            <title>Stories by Richard Hsu on Medium</title>
            <link>https://medium.com/@litehsu?source=rss-245aa36ea0d1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 05:13:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@litehsu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Spring Revisited]]></title>
            <link>https://medium.com/@litehsu/spring-revisited-ad284c086e4?source=rss-245aa36ea0d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad284c086e4</guid>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Richard Hsu]]></dc:creator>
            <pubDate>Tue, 13 Jun 2017 16:14:00 GMT</pubDate>
            <atom:updated>2017-06-13T18:33:16.505Z</atom:updated>
            <content:encoded><![CDATA[<h3>This is my quick summary of reading Spring in Action: 4th Edition</h3><p>Spring has been a corner stone of Java development for quite some time… It allows you to:<br> <br><strong>Manage POJOs via DI (Dependency Injection)</strong></p><p>Without Spring, you would have to new a instance every time. In a spring way, you can list dependency in configuraiton (via annotation or xml bean configuration) Spring will new instances based on configuration with minimal supervision.</p><p><strong>Using of Aspect to make things easier to manage</strong></p><p>It is common to set default behavior accross all your code. Things like security, logging would make the code very hard to manage if you need to wire instances of it into every class. Spring aop allows configuration that allow you to trigger certain code so allow configuration binding rather than code binding.</p><pre>Example<br> &lt;aop:config&gt;<br> &lt;aop:aspect ref=”yourBeanDef”&gt; // define my bean as aspect<br> &lt;aop:pointcut <br> id=”something1&quot; <br> expression=”* *.start(..)” /&gt; //Define point cut</pre><pre>&lt;aop:before pointcut-ref=”something1&quot; method=”doBeforeStart” &gt; // declare before advice</pre><pre>&lt;/aop:aspect&gt;<br> &lt;/aop:config&gt;</pre><p>Wiring beans<br>* configuration in xml<br>* configuration in JAva<br>* implicit bean discovery and automatic wiring<br>* automatic wiring<br>* autowiring</p><pre>* via configuration you can assign bean attribute using p name space:<br> &lt;bean id=”compactDisc”<br> class=”soundsystem.BlankDisc”<br> p:title=”Sgt. Pepper’s Lonely Hearts Club Band”<br> p:artist=”The Beatles”&gt;<br> &lt;property name=”tracks”&gt;<br> &lt;list&gt;<br> &lt;value&gt;list1&lt;/value&gt; <br> &lt;value&gt;Lucy in the Sky with Diamonds&lt;/value&gt;<br> &lt;value&gt;Getting Better&lt;/value&gt;<br> &lt;! — …other tracks omitted for brevity… →<br> &lt;/list&gt;<br> &lt;/property&gt;<br>&lt;/bean&gt;</pre><ul><li>Using List</li></ul><pre>&lt;util:list id=”trackList”&gt;<br> &lt;value&gt;Sgt. Pepper’s Lonely Hearts Club Band&lt;/value&gt;<br> &lt;value&gt;With a Little Help from My Friends&lt;/value&gt;<br> &lt;value&gt;Lucy in the Sky with Diamonds&lt;/value&gt;<br> &lt;value&gt;Getting Better&lt;/value&gt;<br> &lt;value&gt;Fixing a Hole&lt;/value&gt;<br> &lt;! — …other tracks omitted for brevity… →<br>&lt;/util:list&gt;</pre><p>Other than list, there are also constant, Map, properties, properties-path and set.</p><h3>Advanced Wiring</h3><ul><li>Profiles</li><li>Conditional Bean declaration</li><li>Auto wiring and ambiguity</li><li>Bean scoping</li><li>Spring Expression Language</li></ul><h4>Profiles</h4><p>Can set different profile based on need.<br>example: prod, dev, qa</p><pre>&lt;beans profile=”dev”&gt;<br> &lt;bean id=”dataSource”&gt;<br> class = blah<br> p:url = blah …<br> &lt;/bean&gt;<br>&lt;/beans&gt;</pre><ul><li>Conditional Bean declaration</li></ul><pre><a href="http://twitter.com/Bean">@Bean</a><br><a href="http://twitter.com/Conditional">@Conditional</a> (MyCondition.class)<br>Public MagicBean magicBean() {<br> return new MagicBean();<br>}</pre><pre><a href="http://twitter.com/Conditional">@Conditional</a> interface<br>public interface Condition {<br> boolean matches(ConditionContext ctxt,<br> AnnotatedTypeMetadata metadata);<br>}</pre><p>so bean will exist only if Condition classes matches (return true)<br>An implimentation will look like:</p><pre>public class MyCondition impliments Condition {<br> public boolean matches (ConditionContext ctxt,<br> AnnotatedTypeMetadata metadata) {<br> Environment env = ctxt.getEnvironment();<br> return env.containsProperty(“wahaha”);<br> }<br>}</pre><p>in addition, you can use ConditionConext interface which has:</p><pre>public interface ConditionContext {<br> BeanDefinitionRegistry getRegistry();<br> ConfigurableListableBeanFactory getBeanFactory();<br> Environment getEnvironment();<br> ResourceLoader getResourceLoader();<br> ClassLoader getClassLoader();<br>}</pre><p>Autowiring and ambiguity</p><p>When having multiple sub-classes that is declared as <a href="http://twitter.com/Component">@Component</a>, you can have NoUniqueBeanDefinitionException.</p><p>In this case, you can declare bean as primary to reduce confusion. Otherwise you can do <a href="http://twitter.com/Qualifier">@Qualifier</a></p><pre><a href="http://twitter.com/Qualifier">@Qualifier</a>(“yourBeanName”)</pre><p>You can also create your own qualifier as well, <br>but it takes a lot of annotation to set it up.</p><pre><a href="http://twitter.com/YourBeanName">@YourBeanName</a></pre><h4>Bean scoping</h4><ul><li><a href="http://twitter.com/YourBeanName"><strong><em>Singleton</em></strong></a><strong><em> </em></strong>singleton all the way</li><li><a href="http://twitter.com/YourBeanName"><strong><em>Prototype</em></strong></a><strong><em> </em></strong>created only during injected or get from application context</li><li><a href="http://twitter.com/YourBeanName"><strong><em>Session</em></strong></a><strong><em> </em></strong>One bean each session</li><li><a href="http://twitter.com/YourBeanName"><strong><em>Request</em></strong></a><strong><em> </em></strong>bean created for each request</li></ul><p>You can specify by annotation:</p><pre>@Bean<br>@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)<br>public Notepad notepad() {</pre><p>Or bean xml configuration:</p><pre>&lt;bean id=&quot;notepad&quot; class=&quot;com.myapp.Notepad&quot; scope=&quot;prototype&quot; /&gt;</pre><p>Another example on session-based shopping cart:</p><pre>@Component<br>@Scope(<br> value=WebApplicationContext.SCOPE_SESSION,<br> proxyMode=ScopedProxyMode.INTERFACES)<br>public ShoppingCart cart() { … }</pre><h3>Aspect</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*BwevymtmJqb6xXUtY0xS2A.jpeg" /></figure><p>Advice trigger type:</p><ul><li>Before</li><li>After</li><li>After-returning</li><li>After-throwing</li><li>Around</li></ul><h3>Spring Security</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad284c086e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>