<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Developers, Developers, Developers! &#187; injection</title>
	<atom:link href="http://maksim.sorokin.dk/it/tag/injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://maksim.sorokin.dk/it</link>
	<description>Maksim Sorokin IT Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>JPA Persistence With GlassFish</title>
		<link>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/</link>
		<comments>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 22:20:32 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[GlassFish]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=75</guid>
		<description><![CDATA[So you want to use javax.persistence. Here is how you can configure it and run with GlassFish. In this example we will use MySQL database. First, we will need to configure GlassFish JDBC resources. In GlassFish administration portal go to "Resources-&#62;JDBC-&#62;Connection Pools". Create new Connection Pool with name examplePool. Set Resource Type to java.sql.Driver; Database [...]]]></description>
			<content:encoded><![CDATA[<p>So you want to use <code>javax.persistence</code>. Here is how you can configure it and run with GlassFish.<span id="more-75"></span></p>
<p>In this example we will use MySQL database.<br />
First, we will need to configure GlassFish JDBC resources. In GlassFish administration portal go to "Resources-&gt;JDBC-&gt;Connection Pools". Create new Connection Pool with name <code>examplePool</code>. Set Resource Type to <code>java.sql.Driver</code>; Database Vendor to <code>MySql</code>, <code>com.mysql.jdbc.Driver</code>.<br />
Then go to "Resources-&gt;JDBC-&gt;JDBC Resources". Add new resource with JNDI Name <code>jdbc/example</code>. Set Pool Name to <code>examplePool</code>.</p>
<p>In the application under <code>src/main/resource</code> <code>META-INF</code> folder should be created. And inside <code>META-INF</code> folder <code>persistence.xml</code> file added with following content:</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;
    version=&quot;1.0&quot;&gt;
  &lt;persistence-unit name=&quot;default&quot; transaction-type=&quot;JTA&quot;&gt;
    &lt;jta-data-source&gt;&lt;strong&gt;${jtaDataSource}&lt;/strong&gt;&lt;/jta-data-source&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</pre>
<p>Where <code>${jtaDataSource}</code> should be <code>jdbc/example</code>. Or you can enable filtering in Maven <code>pom.xml</code> file and specify <code>jdbc/example</code> there So afterwards it would be easier to change and maintain it:</p>
<pre class="brush: xml; title: ;">
&lt;build&gt;
  ...
  &lt;resources&gt;
    &lt;resource&gt;
      &lt;directory&gt;src/main/resources&lt;/directory&gt;
      &lt;filtering&gt;true&lt;/filtering&gt;
    &lt;/resource&gt;
  &lt;/resources&gt;
  ...
&lt;/build&gt;
...
&lt;properties&gt;
  &lt;jtaDataSource&gt;jdbc/example&lt;/jtaDataSource&gt;
&lt;/properties&gt;
</pre>
<p>You are ready to go! Create Entity:</p>
<pre class="brush: java; title: ;">@Entity
@Table(name = &quot;pencils&quot;)
public class Pencil {
  @Id
  private int id;

  @Column
  private String color;

  public int getId() {
    return id;
  }

  public String getColor() {
    return color;
  }
}
</pre>
<p>Inject <code>javax.persistence.PersistenceContext</code>:</p>
<pre class="brush: java; title: ;">@PersistenceContext
private EntityManager em;</pre>
<p>And you can create queries:</p>
<pre class="brush: java; title: ;">Query q = em.createQuery(&quot;select p from Pencil p where p.color=:color&quot;);
q.setParameter(&quot;color&quot;, &quot;blue&quot;);</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java EE 6 Injection With Dynamic Parameter</title>
		<link>http://maksim.sorokin.dk/it/2010/02/19/java-ee-6-injection-with-dynamic-parameter/</link>
		<comments>http://maksim.sorokin.dk/it/2010/02/19/java-ee-6-injection-with-dynamic-parameter/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 18:08:40 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE 6]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=66</guid>
		<description><![CDATA[Say, you want to inject an object using @javax.inject.Inject. And you would like to pass additional dynamic parameter upon initialization. You can do the following trick. For instance you want to inject and object which depends on some input. In current case let it be Pencil object, which needs to be initialized with color. public [...]]]></description>
			<content:encoded><![CDATA[<p>Say, you want to inject an object using <code>@javax.inject.Inject</code>. And you would like to pass additional dynamic parameter upon initialization. You can do the following trick. <span id="more-66"></span></p>
<p>For instance you want to inject and object which depends on some input. In current case let it be <code>Pencil</code> object, which needs to be initialized with color.</p>
<pre class="brush: java; title: ;">
public class Pencil {
  private Color color;

  void init(Color color) {
    this.color = color;
  }

  public void draw() {
    //
  }
}
</pre>
<p>In this case when you inject the Pencil, you have to call <code>init(Color)</code> explicitly, which is kind off error-prone. What you can do is create a <code>PencilProducer</code> which would create <code>Pencil</code> objects with desired color.</p>
<pre class="brush: java; title: ;">
public class PencilProducer {
  @Inject private Instance pencilInstance;

  public Pencil produce(Colour color) {
    Pencil pencil = pencilInstance.get();
    pencil.init(color);
    return pencil;
  }
}
</pre>
<p>In this case <code>PencilProducer</code> will create new <code>Pencil</code> object every time it is called. And in the application you simply inject <code>PencilProducer</code> and call <code>produce</code> method when needed.</p>
<p>This may seem to be too straight and dirty, but hey -- no <code>new</code> keyword!</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/02/19/java-ee-6-injection-with-dynamic-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

