<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Max DeMaio</title>
    <description>The latest articles on DEV Community by Max DeMaio (@maxdemaio).</description>
    <link>https://dev.to/maxdemaio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F458279%2F699696fd-2235-4e7a-9f6a-80cd3ea8cfb4.jpg</url>
      <title>DEV Community: Max DeMaio</title>
      <link>https://dev.to/maxdemaio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maxdemaio"/>
    <language>en</language>
    <item>
      <title>Intro to React - JSX and Elements</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Wed, 23 Feb 2022 19:23:07 +0000</pubDate>
      <link>https://dev.to/maxdemaio/intro-to-react-jsx-and-elements-3n8k</link>
      <guid>https://dev.to/maxdemaio/intro-to-react-jsx-and-elements-3n8k</guid>
      <description>&lt;p&gt;React is a modern JavaScript library for building interactive user interfaces. In this blog post, we'll get a brief introduction and how to get started with React.&lt;/p&gt;

&lt;p&gt;Demos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/my-demo-react-app-1" rel="noopener noreferrer"&gt;Create React App demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why React?
&lt;/h2&gt;

&lt;p&gt;Let’s look at an Instagram post I’ve made. The moment I “like” a post the status changes. The heart becomes red, the number of likes changes, and we can immediately see this on the web page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.maxdemaio.com%2Fstatic%2Fimages%2Freact-jsx-elements%2Flike.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.maxdemaio.com%2Fstatic%2Fimages%2Freact-jsx-elements%2Flike.png" alt="Instagram post example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instagram has to do the changes by updating the DOM tree of the page and re-rendering the page in the browser. The application also has to show other Instagrammers that I’ve liked this post if they’re looking at it too.&lt;/p&gt;

&lt;p&gt;As of 2019, it was recorded that Instagram had over 1 billion users (Revista Economică, 57). As of the date of this blog post, that number has probably soared to over 2 billion users. Considering the size of Instagram, it'd be a challenge to ensure efficient and consistent DOM manipulation. Luckily, Facebook had already created a frontend library called React specialized in this.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React?
&lt;/h2&gt;

&lt;p&gt;To make it short and snappy, React.js is a JavaScript library. It allows developers to create user interfaces (UIs) and make the development of UI components easy and modular. It was created by Jordan Walke, a software developer at Facebook and it was open sourced to the world by Facebook and Instagram.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Components/JSX: components are the building blocks of React applications. Think of HTML templates but we write them using a special syntax called JSX. After compilation, JSX expressions become JavaScript function calls and evaluate to JavaScript objects. Components can be simple or stateful.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Simple component that will display "Yo {props.name}!"&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;YoMessage&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Yo &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;YoMessage&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"MaxMayo"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yo-example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Classic timer stateful component&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Timer&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="nf"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Virtual DOM: Instead of rebuilding the entire DOM tree for updates, React makes use of the virtual DOM. The virtual DOM is a lightweight copy of the actual DOM. React will update exactly which virtual DOM objects have changed through diffing.&lt;/li&gt;
&lt;li&gt;Unidirectional Data Flow: React has a waterfall like concept of transferring data to other parts of the application. State is passed to the view and child components. Actions are triggered by the view and can update state.&lt;/li&gt;
&lt;li&gt;SEO/Performance: you can even run React on the server for SEO, performance, code sharing and flexibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;We can use create-react-app to bootstrap a React application. The only prerequisite is that we need Node.js version 10+. We can check the Node version in our system with the command &lt;code&gt;node -v&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Off to the races!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
cd my-app
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s a quick explanation of the folder structure generated by create-react-app:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Files&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;node_modules/&lt;/td&gt;
&lt;td&gt;All app dependencies live in this folder&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;public/&lt;/td&gt;
&lt;td&gt;This folder contains the public static assets of the application&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;public/index.html&lt;/td&gt;
&lt;td&gt;This is the first page that gets loaded when we run the application&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;src/&lt;/td&gt;
&lt;td&gt;All application related files/folders are created in this folder&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;src/index.js&lt;/td&gt;
&lt;td&gt;The entry point of the application&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;package.json&lt;/td&gt;
&lt;td&gt;Contains the dependencies of the React application&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we observe the &lt;code&gt;index.js&lt;/code&gt; file we can see the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ReactDOM.render()&lt;/code&gt; renders an element or component to the virtual DOM. The first parameter specifies what needs to be rendered. The second argument specifies where to render. A smaller example without components would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Yo, world!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Babel compiles JSX down to &lt;code&gt;React.createElement()&lt;/code&gt; calls. So these examples are identical:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"yo"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Yo, world!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yo, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike browser DOM elements, React elements are cheap to create because they’re plain JavaScript objects. Our &lt;code&gt;React.render()&lt;/code&gt; would render these React elements since Babel compiles JSX down to &lt;code&gt;React.createElement()&lt;/code&gt; calls. Then, &lt;code&gt;React.createElement()&lt;/code&gt; creates objects aka React elements that generally look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Simplified structure&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yo, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These React elements are representations of what we’d want to see on the screen. Note, elements make up components. React reads these objects and uses them to make the DOM and update it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post we learned about what React is and how to start a React application. In future blog posts I’ll dive into the main concepts of React with helpful demos. Stay tuned!&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;FUCIU, Mircea. “The Rise Of Instagram – Evolution, Statistics, Advantages And Disatvantages.” Lucian Blaga University of Sibiu, Romania, &lt;a href="https://doi.org/http://economice.ulbsibiu.ro/revista.economica/archive/71404fuciu.pdf" rel="noopener noreferrer"&gt;Revista Economică&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;https://reactjs.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Spring Basics - Annotation and Java Based Configuration</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Fri, 18 Feb 2022 03:54:43 +0000</pubDate>
      <link>https://dev.to/maxdemaio/spring-basics-annotation-and-java-based-configuration-2m1g</link>
      <guid>https://dev.to/maxdemaio/spring-basics-annotation-and-java-based-configuration-2m1g</guid>
      <description>&lt;p&gt;In this blog post we’ll learn about annotation and Java based configuration. Goodbye XML configuration! We’ll dive into the ways to autowire dependencies with annotations and their pros and cons. Finally, I’ll give an example using both annotation and Java based configuration.&lt;/p&gt;

&lt;p&gt;Demos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo10-autowire-annotation-property"&gt;annotation configuration on property&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo11-autowire-annotation-constructor"&gt;annotation configuration on constructor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo12-autowire-annotation-setter"&gt;annotation configuration on setter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo13-java-config"&gt;annotated constructor and java based config example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Annotation Based Configuration
&lt;/h2&gt;

&lt;p&gt;All my previous blog posts on Spring have shown how to use XML configuration. But Spring also supports annotation based configuration. This is the preferable way to configure beans in real projects alongside Java based configuration.&lt;/p&gt;

&lt;p&gt;All types of configurations can coexist in the same project. Annotation based configuration reduces the amount of configuration required. Let’s jump into how we can create an application with annotation based configuration!&lt;/p&gt;

&lt;p&gt;Annotation based configuration supports autowiring. We autowire with the annotation &lt;code&gt;@Autowired&lt;/code&gt;. Autowiring injects dependencies based on bean type. This is exactly like &lt;code&gt;byType&lt;/code&gt; mode from XML configuration. Also, auto scanning enables Spring to create beans of the required classes without using explicit &lt;code&gt;&amp;lt;bean&amp;gt;&lt;/code&gt; definitions in the XML file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:context=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/context"&lt;/span&gt;
       &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!--
        &amp;lt;context:component-scan&amp;gt; can also do what
        &amp;lt;context:annotation-config&amp;gt; does in addition to auto scanning.
        with just annotation config you'd still need bean definitions
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;context:component-scan&lt;/span&gt; &lt;span class="na"&gt;base-package=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of &lt;code&gt;&amp;lt;bean&amp;gt;&lt;/code&gt; definitions in the XML file, we annotate our dependencies with the &lt;code&gt;@Component&lt;/code&gt; annotation. This way, Spring can figure out which beans to create during its component/auto scan.&lt;/p&gt;

&lt;p&gt;Now, how would we inject our dependencies into the proper classes? We do this by autowiring with &lt;code&gt;@Autowired&lt;/code&gt;. There are three ways to do so: autowiring by constructor, autowiring the setter method, and autowiring properties/fields.&lt;/p&gt;

&lt;h3&gt;
  
  
  Autowiring on Constructor
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"parameterized constructor"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getwordCount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setwordCount&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="nf"&gt;getgen&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setgen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generatepost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s step through what’s actually happening here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;@Service&lt;/code&gt; annotation is like the &lt;code&gt;@Component&lt;/code&gt; annotation but it’s lexically more specific. This lets Spring know this class relates to the service layer/business logic. This is like adding a &lt;code&gt;&amp;lt;bean&amp;gt;&lt;/code&gt; definition in the XML configuration. Besides being used on the service layer, there isn’t a special use for this.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Autowired&lt;/code&gt; performs autowiring by type for all the arguments in the constructor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Qualifier&lt;/code&gt; specifies which bean I want to use. This is because we have two classes that extend the &lt;code&gt;PostGenerator&lt;/code&gt; interface (see demos provided at beginning). Since autowiring with annotations is by type, Spring will throw an error if it’s unsure of which bean to use.

&lt;ul&gt;
&lt;li&gt;Side note: In enterprise environments, instead of &lt;code&gt;@Qualifier&lt;/code&gt; you can conditionally autowire dependencies with &lt;code&gt;@ConditionalOnProperty&lt;/code&gt; based on environment variables! This way, we could create a stub class and a V1 class and choose which to use based on a value in the environment. This is known as &lt;a href="https://onix-systems.com/blog/introduction-to-feature-flags-in-java-using-the-spring-boot-framework"&gt;feature flagging/toggling/switching&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Value&lt;/code&gt; tells Spring to inject a primitive value. Autowiring isn’t for primitive values so we use this instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benefit of constructor-based injection is that you can declare your injected fields as final. The fields will be initiated during class instantiation. This is good for immutable and required dependencies. Constructor-based injection is recommended for required dependencies allowing them to be immutable and preventing them to be null.&lt;/p&gt;

&lt;h3&gt;
  
  
  Autowiring on Setter Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"120"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"default constructor"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getwordCount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setwordCount&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="nf"&gt;getgen&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setgen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generatepost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In setter based injection, we annotate the setter methods with &lt;code&gt;@Autowired&lt;/code&gt;. Spring will call these setter methods once the Bean is instantiated with the default constructor or a no-argument static factory method in order to inject the dependencies. We can use this for optional dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Autowiring on Property/Field
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getwordCount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setwordCount&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="nf"&gt;getgen&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setgen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generatepost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way of dependency injection should be avoided. In fact, most static analysis tools will even show this method isn’t recommended by the Spring team. But I wanted to show it as an example.&lt;/p&gt;

&lt;p&gt;It’s hard to inject mock objects when our &lt;code&gt;BlogPostService&lt;/code&gt; is under test without a Spring container. With a Spring container you could use &lt;code&gt;@InjectMocks&lt;/code&gt;. The only way to inject these fields outside the Spring container (since we aren’t using setters or constructors) is by reflection. Although elegant because we avoid having excess code, there are drawbacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Java Based Configuration
&lt;/h2&gt;

&lt;p&gt;We saw the above examples of annotation based configuration with the existence of an XML file to enable Spring auto scanning. We don't need an XML file when we use Java based configurations!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@Configuration&lt;/code&gt; annotation tells the Spring container that that Java class contains bean definitions. The &lt;code&gt;@Bean&lt;/code&gt; annotation defines beans within the class annotated with &lt;code&gt;@Configuration&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@ComponentScan&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AppConfig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="n"&gt;srv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBean&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;srv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatepost&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We’ve learned about how to autowire using annotations on constructors, setters, and properties. Also, we can ditch the XML configuration since we now have Java based configuration. We can follow the design philosophies of Spring by using these features of the framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/40830548/spring-autowired-and-qualifier"&gt;Qualifier Annotation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=uO2_ZzIIV70"&gt;Dependency Injection Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=FIACGmzibAM"&gt;Java Reflection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.marcnuri.com/field-injection-is-not-recommended"&gt;Why Field Injection Is Not Recommended&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://onix-systems.com/blog/introduction-to-feature-flags-in-java-using-the-spring-boot-framework"&gt;Feature flagging&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>java</category>
    </item>
    <item>
      <title>Spring Basics - Dependency Injection</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Tue, 01 Feb 2022 19:34:42 +0000</pubDate>
      <link>https://dev.to/maxdemaio/spring-basics-dependency-injection-4mj3</link>
      <guid>https://dev.to/maxdemaio/spring-basics-dependency-injection-4mj3</guid>
      <description>&lt;p&gt;In this blog post we’ll learn about Dependency Injection (DI) and how to use it. We can do this using constructor and setter injection. Also, I recommend checking out this &lt;a href="https://www.youtube.com/watch?v=EPv9-cHEmQw"&gt;YouTube video by Ryan Schachte if it’s still available&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;DI demos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo3-constructor-injection-xml"&gt;constructor injection demo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo4-setter-injection-xml"&gt;setter injection demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;In &lt;a href="https://www.maxdemaio.com/posts/inversion-of-control"&gt;my first Spring basics blog post&lt;/a&gt; we explored how to create Spring beans with XML configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.service.BlogPostService"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Spring with this config would be similar as the below Java code. An instance is created and initialized with the default values using the default constructor. But, if we use Spring with the above config, our code will be loosely coupled!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="n"&gt;blogPostService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also initialize beans with specific values in Spring with DI. There are actually two ways we can do this. We can use constructor injection and setter injection.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Constructor Injection&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Primitive Values&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s consider this BlogPostService class to understand constructor injection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.service&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to define a bean in our XML configuration to initialize values we can write the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.service.BlogPostService"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;constructor-arg&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"8"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/bean&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll need a parameterized constructor and a &lt;code&gt;constructor-arg&lt;/code&gt; in the bean definition. Also, if we have more than one parameter in our constructor, we can pass &lt;code&gt;constructor-arg&lt;/code&gt; tags in the same order.&lt;/p&gt;

&lt;p&gt;We can also use a &lt;code&gt;name&lt;/code&gt; attribute in our &lt;code&gt;constructor-arg&lt;/code&gt; tag to specify constructor parameters. This avoids having to pass arguments in order. We can also specify the &lt;code&gt;type&lt;/code&gt; attribute to avoid order to an extent as well. Spring will convert the String value to the appropriate type if compatible. If it cannot convert the value we’ll get an exception.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Non-Primitive Values&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;So far we’ve seen how we can do constructor injection with primitive data types. Now we’ll take a look at how to do so with non-primitive data types. For example, let’s take our &lt;code&gt;BlogPostService&lt;/code&gt; class. We'll make it dependent upon a &lt;code&gt;PostGenerator&lt;/code&gt; object type to create a “cool” or “witty” blog post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoolPostGenerator&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Generated cool post with "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" words"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WittyPostGenerator&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Generated witty post with "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" words"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.service&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parameterized Constructor"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the &lt;code&gt;PostGenerator&lt;/code&gt; property of the &lt;code&gt;BlogPostService&lt;/code&gt; class has not been initialized with any values. We can let Spring take care of the configuration for us.&lt;/p&gt;

&lt;p&gt;How in the world would we configure this to generate us a “cool” or “witty” blog post? Let’s look at an example where we generate a “cool” blog post using the &lt;code&gt;ref&lt;/code&gt; attribute in our &lt;code&gt;constructor-arg&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.w3.org/2001/XMLSchema-instance&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;
    &amp;lt;http://www.springframework.org/schema/beans/spring-beans.xsd&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.BlogPostService"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;constructor-arg&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"gen"&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;constructor-arg&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"wordCount"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"150"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/bean&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.CoolPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.WittyPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Setter Injection&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For Setter Injection, Spring invokes the setter methods of a class to initialize the properties after invoking its default constructor.&lt;/p&gt;

&lt;p&gt;To start, let’s look at how we can inject primitive values into a class’s properties with setter injection. We'll use the &lt;code&gt;property&lt;/code&gt; tag within the XML configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getwordCount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setwordCount&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="nf"&gt;getgen&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setgen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.w3.org/2001/XMLSchema-instance&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;
    &amp;lt;http://www.springframework.org/schema/beans/spring-beans.xsd&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.BlogPostService"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"gen"&lt;/span&gt; &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"wordCount"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"500"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/bean&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.CoolPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.WittyPostGenerator"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For setter injection, we need a default constructor and setter methods of dependent properties. Spring will use the default constructor to create a bean. Then, Spring invokes the setter method of the respective property. This is based on the &lt;code&gt;name&lt;/code&gt; attribute to initialize the values. Also, &lt;code&gt;property&lt;/code&gt; tags are mandatory in the bean definition.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Collection Values&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We’ve learning how to inject primitive and non-primitive values with the value and ref attributes. To inject collections, Spring supports Java Collection types such as List, Set, Map, and Properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.List&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;gens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setGens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;gens&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PostGenerator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getGens&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;gens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.w3.org/2001/XMLSchema-instance&amp;gt;"&lt;/span&gt;
       &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;http://www.springframework.org/schema/beans&amp;gt;
    &amp;lt;http://www.springframework.org/schema/beans/spring-beans.xsd&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.BlogPostService"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"gens"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;list&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;ref&lt;/span&gt; &lt;span class="na"&gt;bean=&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;ref&lt;/span&gt; &lt;span class="na"&gt;bean=&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/list&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/bean&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"coolPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.CoolPostGenerator"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"wittyPostGenerator"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.demo.WittyPostGenerator"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.demo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.ApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.support.ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"config.xml"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="n"&gt;srv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBean&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;srv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getGens&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We’ve learned how to use constructor and setter injection in Spring. We can do so with primitive, non-primitive, or collection values. To conclude, we’ll take a look at the difference between the two and when you’d want to use one over the other:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Constructor Injection&lt;/th&gt;
&lt;th&gt;Setter Injection&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dependency injection via parameterized constructor&lt;/td&gt;
&lt;td&gt;Dependency Injection via setter methods after invoking the default constructor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need parameterized constructor in the POJO class&lt;/td&gt;
&lt;td&gt;Need default constructor and setter methods in the POJO class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; tag is used in configuration file&lt;/td&gt;
&lt;td&gt; tag is used in configuration file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; tag ref attribute is used to provide dependency for Object type&lt;/td&gt;
&lt;td&gt; tag ref attribute is used to provide dependency for Object type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Good for mandatory dependencies and immutable dependencies. Concise (pass several parameters once).&lt;/td&gt;
&lt;td&gt;Optional/changeable dependencies. Avoids circular dependencies and cycles.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Spring Basics - Inversion of Control</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Mon, 31 Jan 2022 18:15:53 +0000</pubDate>
      <link>https://dev.to/maxdemaio/spring-basics-inversion-of-control-2dn9</link>
      <guid>https://dev.to/maxdemaio/spring-basics-inversion-of-control-2dn9</guid>
      <description>&lt;p&gt;Spring is an open source Java application development framework that supports all types of Java applications such as: enterprise applications, web applications, cloud based applications, and more. Spring applications are simple, easily testable, reusable, and maintainable.&lt;/p&gt;

&lt;p&gt;Spring modules are loosely coupled. Developers can pick and choose modules as needed to build applications.&lt;/p&gt;

&lt;p&gt;Demos for this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo1-blog-post-java"&gt;demo without IoC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/maxdemaio/demos/tree/main/demo2-spring-ioc"&gt;demo with IoC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Introduction to the Spring Framework
&lt;/h2&gt;

&lt;p&gt;To start, I'll give a brief introduction to the features and modules of the Spring framework.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lightweight&lt;/td&gt;
&lt;td&gt;Spring JARs are small. A basic Spring app would be lesser than 10MB. They can be deployed with an embedded Tomcat server and don't require heavy-weight application servers.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Non-Invasive&lt;/td&gt;
&lt;td&gt;Spring apps are developed using POJOs. There's no need to extend or implement any pre-defined classes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Loosely Coupled&lt;/td&gt;
&lt;td&gt;Spring uses Dependency Injection and Aspect Oriented Programming to facilitate loosely coupled code.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inversion of Control (IoC)&lt;/td&gt;
&lt;td&gt;IoC takes care of the application objects’ life cycles along with their dependencies.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spring Container&lt;/td&gt;
&lt;td&gt;Spring container takes care of object creation, initialization, and managing object dependencies.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Aspect Oriented Programming (AOP)&lt;/td&gt;
&lt;td&gt;Promotes separation of supporting functions (concerns) such as logging, transaction, and security from the core business logic of the application.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Spring 5.x has the following key module groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Core Container: core modules that provide key features of the Spring framework.&lt;/li&gt;
&lt;li&gt;Data Access/Integration: These modules support JDBC and ORM data access&lt;/li&gt;
&lt;li&gt;Web: these modules provide support for web applications.&lt;/li&gt;
&lt;li&gt;Others: Spring also provides other modules such as Test for testing Spring applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x7yWCeMx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvk1qpph9eqs0e9gyov1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x7yWCeMx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvk1qpph9eqs0e9gyov1.png" alt="Spring modules" width="720" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Spring Inversion of Control
&lt;/h2&gt;

&lt;p&gt;Usually, it's the developer's responsibility to create the dependent application objects using the &lt;code&gt;new&lt;/code&gt; operator. Hence any change in the application dependencies requires code changes. This results in tight coupling and more complexity as the application grows bigger.&lt;/p&gt;

&lt;p&gt;Inversion of Control (IoC) creates a more loosely coupled application. IoC inverts the responsibility of the application objects’ creation, initialization, and destruction from the application to a third-party aka the framework. The third party will take care of application object management and dependencies. This makes an application easy to maintain, test, and reuse. The Spring framework provides IoC implementation using dependency injection (DI).&lt;/p&gt;

&lt;p&gt;Objects managed by Spring are called beans. Thanks to Spring, we don’t need to create objects ourselves. Dependency injection allows us to describe how objects should be created through configuration.&lt;/p&gt;

&lt;p&gt;DI is a software design pattern that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Helps to create loosely coupled application architecture facilitating re-usability and easy testing.&lt;/li&gt;
&lt;li&gt;Separates responsibility by keeping code and configuration apart. Dependencies can be easily modified using configuration without changing the code.&lt;/li&gt;
&lt;li&gt;Allows us to replace actual objects with mock objects for testing. This improves testability by writing simple JUnit tests that use mock objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned before, the core container module of the Spring framework provides IoC using dependency injection. The Spring container knows which objects to create and when to create them through the additional details that we provide in our application called configuration metadata.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kI_iBT7Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ell6jqwppy3ceha1a97z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kI_iBT7Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ell6jqwppy3ceha1a97z.png" alt="Spring config" width="498" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two types of containers Spring provides: BeanFactory and ApplicationContext (interfaces). The ApplicationContext is the preferred container for development and inherits from BeanFactory. ApplicationContext provides added features to support enterprise services such as internationalization, validation, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// org.springframework.context.support.ClassPathXmlApplicationContext&lt;/span&gt;
&lt;span class="c1"&gt;// is the most common implementation of ApplicationContext&lt;/span&gt;
&lt;span class="nc"&gt;ApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"config.xml"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBean&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"exampleService"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;BeanFactory&lt;/th&gt;
&lt;th&gt;ApplicationContext&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No annotation based DI support.&lt;/td&gt;
&lt;td&gt;Annotation based DI support.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No enterprise service support.&lt;/td&gt;
&lt;td&gt;Enterprise service support: validations, internationalization, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Supports Lazy Loading by default&lt;/td&gt;
&lt;td&gt;Supports Eager Loading by default. Beans are instantiated during load time.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Spring allows multiple methods to configure the metadata into our POJOs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;XML configuration&lt;/li&gt;
&lt;li&gt;Annotation based configuration&lt;/li&gt;
&lt;li&gt;Java based configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To conclude, I will introduce a small IoC example with XML based configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.service&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hi, Welcome to the Blog Post Generation application"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Example object in a config.xml file --&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!--
1. &amp;lt;beans&amp;gt; is the root element &amp;amp; also includes namespace declarations

2. Bean definition

3. id attribute represents a unique bean identifier

4. class attribute represents a fully qualified class name
--&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;beans&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans"&lt;/span&gt;
 &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt;
 &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;bean&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"com.maxdemaio.service.BlogPostService"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.client&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.ApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.support.ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.maxdemaio.service.BlogPostService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

 &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

 &lt;span class="c1"&gt;// ApplicationContext container is instantiated by loading the configuration&lt;/span&gt;
 &lt;span class="c1"&gt;// from config.xml available in application classpath&lt;/span&gt;
 &lt;span class="nc"&gt;ApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"config.xml"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;// Access bean with id “blogPostService"&lt;/span&gt;
 &lt;span class="c1"&gt;// Typecast from Object type to blogPostService type&lt;/span&gt;
 &lt;span class="nc"&gt;BlogPostService&lt;/span&gt; &lt;span class="n"&gt;blogPostService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPostService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBean&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"blogPostService"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
 &lt;span class="c1"&gt;// Invoke display method of blogPostService to display greeting on console&lt;/span&gt;
        &lt;span class="n"&gt;blogPostService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rather than using the &lt;code&gt;new&lt;/code&gt; keyword, we configured a managed Spring bean to take its place. This allows us to have loosely coupled code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.spring.io/spring-framework/docs/5.0.0.RC2/spring-framework-reference/overview.html"&gt;https://docs.spring.io/spring-framework/docs/5.0.0.RC2/spring-framework-reference/overview.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Next.js and Docker</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Mon, 31 Jan 2022 18:05:32 +0000</pubDate>
      <link>https://dev.to/maxdemaio/nextjs-and-docker-2496</link>
      <guid>https://dev.to/maxdemaio/nextjs-and-docker-2496</guid>
      <description>&lt;p&gt;In this blog post I'm introducing a dangerously good application development duo, Next.js and Docker. To start, I'll briefly go over both Next.js and Docker. After, I'll explain how you can combine these two technologies. Let's get this party &lt;code&gt;docker start&lt;/code&gt;ed!&lt;/p&gt;




&lt;h2&gt;
  
  
  Next.js
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt; is an open-source React framework built on top of Node.js. Next.js leverages a wide array of functionalities such as hybrid static &amp;amp; server rendering, TypeScript support, smart bundling, route pre-fetching, and more. In fact, &lt;a href="https://github.com/maxdemaio/max-nextjs"&gt;my website&lt;/a&gt; was built using Next.js!&lt;/p&gt;

&lt;p&gt;Curious about giving it a try? Next.js has a &lt;a href="https://nextjs.org/learn/basics/create-nextjs-app"&gt;step-by-step tutorial&lt;/a&gt; on building your first app. To get a Next.js app up and running in seconds you can use &lt;code&gt;npx create-next-app@latest&lt;/code&gt; or &lt;code&gt;yarn create next-app&lt;/code&gt;. Also, if you get stuck you can visit the &lt;a href="https://nextjs.org/docs/api-reference/create-next-app"&gt;Create Next App documentation&lt;/a&gt;. There are even &lt;a href="https://github.com/vercel/next.js/tree/canary/examples"&gt;official Next.js templates&lt;/a&gt; you can snag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; is an open platform for developing, shipping, and running apps. Before Docker, you had to locally set up your app and all of its dependencies according to your machine. Docker eliminates this issue by packaging and running apps in loosely isolated environments called containers.&lt;/p&gt;

&lt;p&gt;Docker also has a &lt;a href="https://docs.docker.com/get-started/overview/"&gt;great overview on their website&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Command Starter Kit
&lt;/h3&gt;

&lt;p&gt;Here are the most useful commands I found to get you up and running with Docker. For more detailed information on each command and all other commands you can visit &lt;a href="https://docs.docker.com/engine/reference/commandline/docker/"&gt;Docker's documentation&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker login&lt;/code&gt; - log in to local registry or Docker Hub&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker pull IMAGE&lt;/code&gt; - pulls an image from Docker Hub (default registry), but you can specify a different one&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker push IMAGE&lt;/code&gt; - pushes an image to Docker Hub (default registry), but you can specify different one&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker images&lt;/code&gt; - gives you a list of images on the host machine&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker run IMAGE&lt;/code&gt; - creates a Docker container of the specified Docker image and starts it in the current terminal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker stop CONTAINER&lt;/code&gt; - stops a given container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker start CONTAINER&lt;/code&gt; - starts a given container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker ps&lt;/code&gt; - status of all running containers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker ps -a&lt;/code&gt; - status of all running and not running containers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker logs CONTAINER&lt;/code&gt; - gives the logs of a given container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker exec [OPTIONS] CONTAINER COMMAND [ARG...]&lt;/code&gt; - runs a command in a running container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker network ls&lt;/code&gt; - see all available Docker networks&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker network create example-network&lt;/code&gt; - creates a network&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker build -t IMAGE:tag dockerfile-location&lt;/code&gt; - builds an image from the specified Dockerfile and will then tag it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker rm CONTAINER&lt;/code&gt; - deletes a container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker rmi image&lt;/code&gt; - deletes an image&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker version&lt;/code&gt; - provides docker version information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Next.js and Docker
&lt;/h2&gt;

&lt;p&gt;Let's say you've crafted yourself a performant, stunning, and interactive Next.js application.&lt;/p&gt;

&lt;p&gt;Once you create a Dockerfile for your Next.js application, you can use it to create a Docker image and then a Docker container. To create an image, we use &lt;code&gt;docker build&lt;/code&gt; and then to create a container we use &lt;code&gt;docker run&lt;/code&gt;. Take for example this Dockerfile I use to create an image of my website which should apply to most Next.js apps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Building image&lt;/span&gt;
&lt;span class="c"&gt;# docker build . -t example-image-name&lt;/span&gt;

&lt;span class="c"&gt;# Install dependencies only when needed&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:14-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;deps&lt;/span&gt;
&lt;span class="c"&gt;# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apk add &lt;span class="nt"&gt;--no-cache&lt;/span&gt; libc6-compat
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--frozen-lockfile&lt;/span&gt;

&lt;span class="c"&gt;# Rebuild the source code only when needed&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:14-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=deps /app/node_modules ./node_modules&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="c"&gt;# Production image, copy all the files and run next&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:14-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;runner&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; NODE_ENV production&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;addgroup &lt;span class="nt"&gt;-g&lt;/span&gt; 1001 &lt;span class="nt"&gt;-S&lt;/span&gt; nodejs
&lt;span class="k"&gt;RUN &lt;/span&gt;adduser &lt;span class="nt"&gt;-S&lt;/span&gt; nextjs &lt;span class="nt"&gt;-u&lt;/span&gt; 1001

&lt;span class="c"&gt;# You only need to copy next.config.js if you are NOT using the default configuration&lt;/span&gt;
&lt;span class="c"&gt;# COPY --from=builder /app/next.config.js ./&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /app/public ./public&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder --chown=nextjs:nodejs /app/.next ./.next&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /app/node_modules ./node_modules&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /app/package.json ./package.json&lt;/span&gt;

&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; nextjs&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PORT 3000&lt;/span&gt;

&lt;span class="c"&gt;# Next.js collects completely anonymous telemetry data about general usage.&lt;/span&gt;
&lt;span class="c"&gt;# Learn more here: https://nextjs.org/telemetry&lt;/span&gt;
&lt;span class="c"&gt;# Uncomment the following line in case you want to disable telemetry.&lt;/span&gt;
&lt;span class="c"&gt;# ENV NEXT_TELEMETRY_DISABLED 1&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node_modules/.bin/next", "start"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see how the Dockerfile's instructions create layers to form the resulting image.&lt;/p&gt;

&lt;p&gt;Consider this set of instructions where each instruction creates one layer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM&lt;/code&gt; creates a layer from the &lt;code&gt;node:14-alpine&lt;/code&gt; Docker image&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN&lt;/code&gt; &lt;a href="https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine"&gt;adds missing shared libraries to our image&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR&lt;/code&gt; sets the working directory for any subsequent instructions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY&lt;/code&gt; adds our dependency blueprints (package.json/yarn.lock) from the Docker client’s current directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN&lt;/code&gt; installs all of our Next.js application's dependencies with yarn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's use this Dockerfile to get our Next.js app running on a container. Place the Dockerfile in the outermost directory of your Next.js application. Now, we can run &lt;code&gt;docker build . -t example-image-name&lt;/code&gt; in that outermost directory of the Next.js application to forge an image! Finally with the command &lt;code&gt;docker run -p3000:3000 example-image-name&lt;/code&gt; you can create a container. After using the &lt;code&gt;docker run&lt;/code&gt; command you can actually view your app running on the container. With your image and container you are poised to deploy to any Docker hosting platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To summarize, we've learned about what Next.js and Docker are and how you can use them together. Next.js is a leading frontend framework with a phenomenal developer experience. Docker is an open platform for development and ops best practices. Docker also has collaboration features such as &lt;code&gt;docker push&lt;/code&gt; and &lt;code&gt;docker pull&lt;/code&gt; to run images on any machine. If you're looking to share and deploy beautiful frontend applications, these two technologies definitely are the life of the party.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>docker</category>
      <category>programming</category>
    </item>
    <item>
      <title>Spring Data Transfer Objects</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Sun, 30 Jan 2022 19:45:29 +0000</pubDate>
      <link>https://dev.to/maxdemaio/spring-data-transfer-objects-4n5g</link>
      <guid>https://dev.to/maxdemaio/spring-data-transfer-objects-4n5g</guid>
      <description>&lt;p&gt;Data Transfer Objects (DTOs) carry data between processes. Each call to an application is expensive so we should try to keep them to a minimum. DTOs help solve this pesky problem! DTOs need to be serializable to go across remote connections. Usually, an assembler is used on the server side to transfer data between DTOs and any domain objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;DTO design patterns are frequently used in enterprise applications. Why? This design pattern allows us to pass data with multiple attributes in one shot from client to server, avoiding multiple calls to an application. Also, we avoid exposing the implementation details of our domain objects pulled directly from the repository layer (direct object-relational mapping from our databases to Java objects).&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;When writing RESTful APIs we can even analyze an incoming request and stop it in its tracks by validating its request body data against our DTOs. This saves precious I/O since you can identify validation errors before you even get past the controller layer. I've created an &lt;a href="https://github.com/maxdemaio/demos/tree/main/dtoExample"&gt;example application&lt;/a&gt; where I demonstrate applying validation constraints to a DTO class which I will detail here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonDTO&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;
    &lt;span class="nd"&gt;@Size&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;
    &lt;span class="nd"&gt;@Min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Person(Name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", Age: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;")"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see we've flagged this class’s attributes with a few standard validation annotations. Now, let's make an example controller to demonstrate how we can stop a request in its tracks before traveling to our service layer if there are any mistakes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;checkPersonInfo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Valid&lt;/span&gt; &lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;PersonDTO&lt;/span&gt; &lt;span class="n"&gt;personDTO&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Valid person DTO!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example application's repository, I've also included two Postman API calls. If we were to make a POST request with invalid age or name data, the end user receives a 400 error. However, if all fields of the request body of the POST request are valid, we receive a nice message "Valid person DTO!". In a real application, catching these types of errors early is extremely important.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We now know what DTOs are used for and why they're very helpful in enterprise applications. This design pattern is very common and allows for efficient API architecture when coupled with validation. In short: validate, validate, validate!&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tailwind, React, and TypeScript - How to Get Started</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Sun, 30 Jan 2022 19:25:11 +0000</pubDate>
      <link>https://dev.to/maxdemaio/tailwind-react-and-typescript-how-to-get-started-4lj7</link>
      <guid>https://dev.to/maxdemaio/tailwind-react-and-typescript-how-to-get-started-4lj7</guid>
      <description>&lt;p&gt;Want to construct your frontend with Tailwind, React, and Typescript? Look no further; here we will discuss everything that you'll need to get setup.&lt;/p&gt;




&lt;h2&gt;
  
  
  React and Typescript setup
&lt;/h2&gt;

&lt;p&gt;Before we get started, "Tailwind CSS requires Node.js 12.13.0 or higher" (tailwindcss). Make sure you have Node.js installed and the correct version by running &lt;code&gt;node --version&lt;/code&gt; in your command line. If you don't have it installed, feel free to visit &lt;a href="https://nodejs.org/en/"&gt;Node.js's official website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now let's get down to business, creating a new React project with TypeScript using &lt;a href="https://create-react-app.dev/"&gt;Create React App&lt;/a&gt;. The way we can bootstrap a new React project with TypeScript according to the &lt;a href="https://create-react-app.dev/docs/adding-typescript/"&gt;Create React App documentation&lt;/a&gt; is &lt;code&gt;npx create-react-app my-app --template typescript&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Tailwind CSS with Create React App
&lt;/h2&gt;

&lt;p&gt;Your React + TypeScript project has now been made and all that's left is to install Tailwind CSS. To do so, we have to follow some steps according to &lt;a href="https://tailwindcss.com/docs/guides/create-react-app"&gt;Tailwind CSS's Create React App installation documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://tailwindcss.com/docs/guides/create-react-app#install-and-configure-craco"&gt;Install Tailwind CSS via npm&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm install -D tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://tailwindcss.com/docs/guides/create-react-app#install-and-configure-craco"&gt;Install and configure CRACO&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm install @craco/craco&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once CRACO has finished installing, edit your &lt;code&gt;package.json&lt;/code&gt; to use &lt;code&gt;craco&lt;/code&gt; for all scripts except &lt;code&gt;eject&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"craco start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"craco build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"craco test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"eject"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react-scripts eject"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;craco.config.js&lt;/code&gt; file at the root of your React project, adding in the &lt;code&gt;tailwindcss&lt;/code&gt; and &lt;code&gt;autoprefixer&lt;/code&gt; PostCSS plugins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// craco.config.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;postcss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tailwindcss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;autoprefixer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://tailwindcss.com/docs/guides/create-react-app#create-your-configuration-file"&gt;Create your configuration file&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npx tailwindcss-cli@latest init&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://tailwindcss.com/docs/guides/create-react-app#include-tailwind-in-your-css"&gt;Include Tailwind in your CSS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Change the &lt;code&gt;index.css&lt;/code&gt; file located in the &lt;code&gt;src&lt;/code&gt; directory in the root of your React project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* ./src/index.css */&lt;/span&gt;
&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You're all set! You've successfully configured a React project to use TypeScript and Tailwind CSS. I hope this blog post helped you get your new frontend set up to create beautiful UI and UX.&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;“Install Tailwind CSS with Create React App” &lt;em&gt;tailwindcss&lt;/em&gt;, &lt;a href="https://tailwindcss.com/docs/guides/create-react-app"&gt;https://tailwindcss.com/docs/guides/create-react-app&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;“Adding TypeScript” &lt;em&gt;Create React App&lt;/em&gt;, &lt;a href="https://create-react-app.dev/docs/adding-typescript/"&gt;https://create-react-app.dev/docs/adding-typescript/&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tutorial</category>
      <category>typescript</category>
      <category>tailwindcss</category>
      <category>react</category>
    </item>
    <item>
      <title>Syntax - The Science of Sentences</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Sat, 07 Aug 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/maxdemaio/syntax-the-science-of-sentences-b1h</link>
      <guid>https://dev.to/maxdemaio/syntax-the-science-of-sentences-b1h</guid>
      <description>&lt;p&gt;The science of sentences and the relationships between words. Syntax is broadly understood as the study of word order and how words are put together into larger units.&lt;/p&gt;




&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Most core questions in the scientific domain of linguistics are in these three areas alone (Kumar, 00:00:54 - 00:01:30):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structure&lt;/li&gt;
&lt;li&gt;Acquisition&lt;/li&gt;
&lt;li&gt;Change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax focuses on the structure of language and the observation of &lt;a href="https://en.wikipedia.org/wiki/Word_order"&gt;how words are ordered&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s take the sentence &lt;code&gt;Max likes Pokémon&lt;/code&gt;. Looking at each word’s categorical features, the subject of the sentence is &lt;code&gt;Max&lt;/code&gt;, the verb is &lt;code&gt;like&lt;/code&gt;, and the object is &lt;code&gt;Pokémon&lt;/code&gt;. There is a grammatical relationship between the &lt;a href="https://en.wikipedia.org/wiki/Constituent_(linguistics)"&gt;constituents&lt;/a&gt;. However, these grammatical relationships are only important within sentences. For example, &lt;code&gt;Max&lt;/code&gt; (a noun) by itself isn’t a subject, but when placed in our example sentence it is. You can also note the fact that in English the word sequence is subject, verb, and then object (SVO).&lt;/p&gt;

&lt;h2&gt;
  
  
  Word sequence
&lt;/h2&gt;

&lt;p&gt;For all the languages that exist on Planet Earth there is a commonality; sentences have some sequence of word order. We can classify this into how subjects, objects, and verbs interact (edge case: ergative languages can have ambiguous subjects and have &lt;a href="https://en.wikipedia.org/wiki/Ergative%E2%80%93absolutive_alignment"&gt;agents&lt;/a&gt; instead). Another cool note is some languages can even have &lt;a href="https://en.wikipedia.org/wiki/Null-subject_language"&gt;null subjects&lt;/a&gt;. Let’s check out all the combinations using Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get all syntax sequence permutations and print
from itertools import permutations

perm = permutations(['S', 'O', 'V'])
for i in list(perm):
    print(i)


('s', 'o', 'v')
('s', 'v', 'o')
('o', 's', 'v')
('o', 'v', 's')
('v', 's', 'o')
('v', 'o', 's')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modern English (SVO): Pat hit Alex&lt;/li&gt;
&lt;li&gt;Basque (SOV/AOV): Pat-ek Alex egurtu&lt;/li&gt;
&lt;li&gt;Welsh (VSO): Fe darodd Pat Alex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not all languages fall into the bucket of sticking to a specific word pattern. For example, languages like Russian, Korean, Hungarian, and many others have existing grammar rules that allow one to &lt;a href="https://en.wikipedia.org/wiki/Scrambling_(linguistics)"&gt;change sentence structure&lt;/a&gt;. In reality some languages can have a semi-fluid or fluid word order. These sentence structure changes are highly flexible and reflect the &lt;a href="https://plato.stanford.edu/entries/pragmatics/"&gt;pragmatics&lt;/a&gt; of the utterance.&lt;/p&gt;

&lt;p&gt;Morphemes can also help distinguish a word’s relationship in a sentence. For example, in Latin the sentence &lt;code&gt;hospes leporem videt&lt;/code&gt; and &lt;code&gt;hospitem lepus videt&lt;/code&gt; have the same word order but opposite meanings. The first sentence translates to &lt;code&gt;the host sees the rabbit&lt;/code&gt; and the second translates to &lt;code&gt;the rabbit sees the host&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I’d like to also touch upon the difference between grammar and syntax. You might be thinking, what is the difference between grammar and syntax? Well, grammar is a rather wide field which includes syntax. In theoretical linguistics grammar includes syntax, morphology, phonology, phonetics, and semantics (the entire system of spoken language). Without grammar there is no syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntactic models
&lt;/h2&gt;

&lt;p&gt;When researching syntactic models, I realized there are many fields that are progressing on this subject. These models help provide a means to approach how syntactic units and constituents are arranged and represented. The main syntactic models are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Syntax"&gt;All syntactic models from Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Dependency_grammar"&gt;Dependency grammar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Categorial_grammar"&gt;Categorical grammar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Stochastic_grammar"&gt;Stochastic/probabilistic grammars/network theories&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Statistical_machine_translation"&gt;Statistical machine translation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Functional_linguistics"&gt;Functional grammars&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Generative_grammar"&gt;Generative grammar&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have dove into the overall understanding of what syntax is as a focus of study. Each syntactic model has an ocean of research behind it to try and understand how syntactic units are categorized in sentences. Every language is different whether it be spoken or signed, but all of them allow for grammatically correct word orders to convey meaning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;nptelhrd (Prof. Rajesh Kumar). “Mod-01 Lec-27 Syntax: An Introduction” &lt;em&gt;YouTube&lt;/em&gt;, 14 Nov. 2014, &lt;a href="https://youtu.be/MYjsn5JtaSg"&gt;https://youtu.be/MYjsn5JtaSg&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;nptelhrd (Prof. Rajesh Kumar). “Mod-01 Lec-28 Syntax: An Introduction” &lt;em&gt;YouTube&lt;/em&gt;, 14 Nov. 2014, &lt;a href="https://youtu.be/06AVRjc0Z6Q"&gt;https://youtu.be/06AVRjc0Z6Q&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CrashCourse. “Syntax 1 - Morphosyntax: Crash Course Linguistics #3” &lt;em&gt;YouTube&lt;/em&gt;, 25 Sept. 2020, &lt;a href="https://youtu.be/B1r1grQiLdk"&gt;https://youtu.be/B1r1grQiLdk&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CrashCourse. “Syntax 2 - Trees: Crash Course Linguistics #4” &lt;em&gt;YouTube&lt;/em&gt;, 2 Oct. 2020, &lt;a href="https://youtu.be/n1zpnN-6pZQ"&gt;https://youtu.be/n1zpnN-6pZQ&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“Word order” &lt;em&gt;Wikipedia&lt;/em&gt;, 10 June 2021, &lt;a href="https://en.wikipedia.org/wiki/Word_order"&gt;https://en.wikipedia.org/wiki/Word_order&lt;/a&gt;. Accessed 4 July 2021.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“Syntax” &lt;em&gt;Wikipedia&lt;/em&gt;, 25 June 2021, &lt;a href="https://en.wikipedia.org/wiki/Syntax"&gt;https://en.wikipedia.org/wiki/Syntax&lt;/a&gt;. Accessed 4 July 2021.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>linguistics</category>
      <category>language</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>MySQL CRUD Basics - Creating a Database</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Wed, 14 Jul 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/maxdemaio/mysql-crud-basics-creating-a-database-241e</link>
      <guid>https://dev.to/maxdemaio/mysql-crud-basics-creating-a-database-241e</guid>
      <description>&lt;p&gt;Structured Query Language (SQL) is used to manage data in all relational database management systems such as MySQL, Oracle, SQL Server, and more. SQL standards are maintained by &lt;a href="https://en.wikipedia.org/wiki/ISO/IEC_9075"&gt;ISO&lt;/a&gt;. While most database products comply with the ISO standard, they also offer additional proprietary features. In this blog post we’re going to restrict ourselves to the feature set offered by &lt;a href="https://www.mysql.com/"&gt;MySQL&lt;/a&gt; databases.&lt;/p&gt;




&lt;h2&gt;
  
  
  MySQL data types and operators
&lt;/h2&gt;

&lt;p&gt;Each column in a database table is required to have a name and a data type. When designing databases we have to decide what kind of data will be stored in its database tables. Data types allow SQL to understand and interact with the stored data within database table columns. MySQL supports SQL data types in multiple categories: numeric types, date and time types, string (character and byte) types, spatial types, and the JSON data type.&lt;/p&gt;

&lt;p&gt;Also, it’s noteworthy to mention the operators that we can use in SQL expressions. Operators can be broken into three main categories: arithmetic, comparison, and logical.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple MySQL example
&lt;/h2&gt;

&lt;p&gt;Imagine that you own a business. This business sells video games via its website and also via its physical store location. Customers can place orders with employees on-site or buy video games online without an employee’s assistance. The order table links both employees and customers, however each order doesn’t necessarily need an employee foreign key as mentioned before! A database and tables would help us manage all of this information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8xoQB7EU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://maxwelldemaio.github.io/blog/assets/overflowDB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8xoQB7EU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://maxwelldemaio.github.io/blog/assets/overflowDB.png" alt="Database and tables example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How would we create and populate the database and tables pictured above? It’s quite straightforward, don’t worry. Also, I’ll dive into how to read data and use update/delete statements to alter data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a database and tables
&lt;/h2&gt;

&lt;p&gt;In order to start working with databases we need to create one. With the &lt;code&gt;CREATE DATABASE&lt;/code&gt; statement we can do just that. Interacting with the database is as simple as using the &lt;code&gt;USE DATABASE&lt;/code&gt; statement followed by any other statements you’d like.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CREATE TABLE&lt;/code&gt; statement allows us to create a table with a given name. There are &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/create-table.html"&gt;many aspects&lt;/a&gt; to the &lt;code&gt;CREATE TABLE&lt;/code&gt; statement described under the following topics: table name, temporary tables, table cloning and copying, column data types and attributes, indexes/foreign keys/check constraints, table options, and table partitioning. Let’s put this into practice and create a database with tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE MaxOverflowExample;
USE MaxOverflowExample;

CREATE TABLE Customer ( 
    CustNo CHAR(8),
    CustFirstName   VARCHAR(20) NOT NULL,
    CustLastName    VARCHAR(30) NOT NULL,
    CustCity VARCHAR(30),
    CustState   CHAR(2),
    CustZip CHAR(10),
    CustBal DECIMAL(12,2),
    CONSTRAINT PKCustomer PRIMARY KEY (CustNo)  
 );

 CREATE TABLE Employee( 
    EmpNo CHAR(8),
    EmpFirstName VARCHAR(20) NOT NULL,
    EmpLastName VARCHAR(30) NOT NULL,
    EmpPhone CHAR(15),
    EmpEMail VARCHAR(50) NOT NULL,
    CONSTRAINT PKEmployee PRIMARY KEY (EmpNo),
    CONSTRAINT UniqueEMail UNIQUE (EmpEMail) 
);

CREATE TABLE OrderTbl ( 
    OrdNo CHAR(8),
    OrdDate DATE NOT NULL,
    CustNo CHAR(8) NOT NULL,
    EmpNo   CHAR(8),
    CONSTRAINT PKOrderTbl PRIMARY KEY (OrdNo) ,
    CONSTRAINT FKCustNo FOREIGN KEY (CustNo) REFERENCES Customer (CustNo),
    CONSTRAINT FKEmpNo FOREIGN KEY (EmpNo) REFERENCES Employee (EmpNo)
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, each table has a unique primary key column to identify each table row. In the order table, there are two foreign key columns to link back to employees that sold customers video games at our physical store. Moreover, the order table tracks all orders for us including online orders.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inserting data
&lt;/h2&gt;

&lt;p&gt;Let’s insert some data into these bad boys. This is done via the &lt;code&gt;INSERT INTO&lt;/code&gt; statement. When we use an &lt;code&gt;INSERT&lt;/code&gt; statement, we have to match up our data to conform to the data types and constraints we defined in our &lt;code&gt;CREATE TABLE&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Note the order I insert the data as well. I insert data into the employee and customer tables first since the order table contains rows that reference back to those entries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE MaxOverflowExample;

INSERT INTO Customer
    (CustNo, CustFirstName, CustLastName, CustCity, CustState, CustZip, CustBal) 
    VALUES('C0954327','Sheri','Gordon','Littleton','CO','80129-5543',230.00);

INSERT INTO Employee
    (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEMail)
    VALUES ('E8544399','Joe','Jenkins','(303) 221-9875','JJenkins@bigco.com');

INSERT INTO OrderTbl 
    (OrdNo, OrdDate, CustNo, EmpNo)
    VALUES ('O9919699','2017-02-11','C0954327','E8544399');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reading data
&lt;/h2&gt;

&lt;p&gt;We can query our database tables using the &lt;code&gt;SELECT&lt;/code&gt; statement. &lt;code&gt;SELECT&lt;/code&gt; queries can become quite complex when dealing with a production level database. Here, I demonstrate how you can get all the data from all three tables we created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE MaxOverflowExample;

SELECT * FROM Customer;
SELECT * FROM Employee;
SELECT * FROM Ordertbl;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Updating and deleting data
&lt;/h2&gt;

&lt;p&gt;For updating and deleting data in our database tables we can use the &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; statements. All that’s needed is to specify the primary key of the row we’d like to either update or delete.&lt;/p&gt;

&lt;p&gt;Sidenote: the variable “SQL_SAFE_UPDATES” is enabled by default. This prevents the MySQL database engine from executing &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; statements that don’t include WHERE clauses that reference the primary key column(s). This saves you from affecting every row in the given table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE MaxOverflowExample;

-- Update customer first name
UPDATE Customer
  SET CustFirstName = 'George'
  WHERE CustNo = 'C0954327';

-- Delete rows added
DELETE FROM OrderTbl
  WHERE OrdNo = 'O9919699';

DELETE FROM Employee
  WHERE EmpNo = 'E8544399';

DELETE FROM Customer
  WHERE CustNo = 'C0954327';

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I hope this post helped boost your MySQL &lt;a href="https://dragonball.fandom.com/wiki/Power_Level"&gt;power level&lt;/a&gt;. If you like video format better, I have a &lt;a href="https://www.youtube.com/playlist?list=PLg7mHz5jVDueWom70v1JjpaCfxwh99_fC"&gt;YouTube playlist&lt;/a&gt; where I demonstrate everything in the MySQL workbench. MySQL is a database management system that allows us to interact with and design databases. With a solid foundation of MySQL basics, you can start designing and interacting with databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;“MySQL 8.0 Reference Manual.” &lt;em&gt;MySQL&lt;/em&gt;, &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/"&gt;https://dev.mysql.com/doc/refman/8.0/en/&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>mysql</category>
      <category>database</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Morphology - How Words Are Studied</title>
      <dc:creator>Max DeMaio</dc:creator>
      <pubDate>Sun, 04 Jul 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/maxdemaio/morphology-how-words-are-studied-4ado</link>
      <guid>https://dev.to/maxdemaio/morphology-how-words-are-studied-4ado</guid>
      <description>&lt;p&gt;The study of words, how they are constructed, and their relationship to other words in the same language.&lt;/p&gt;




&lt;p&gt;What even is a word? Well, for each word we’d expect to find its entry in a dictionary. Meanings of words can be unpredictable and each word is known as a lexeme or lexical item since their key/value pairs are in a lexicon (dictionary). The words we use are arbitrary &lt;a href="https://en.wikipedia.org/wiki/Sign_(semiotics)"&gt;signs&lt;/a&gt; that are essentially &lt;a href="https://en.wikipedia.org/wiki/Pointer_(computer_programming)"&gt;pointers&lt;/a&gt; to objects/ideas in the world.&lt;/p&gt;

&lt;p&gt;Now, let’s take the words &lt;code&gt;cat&lt;/code&gt; and &lt;code&gt;cats&lt;/code&gt;. These words are quite similar and are only different by the plurality &lt;a href="https://en.wikipedia.org/wiki/Morpheme"&gt;morpheme&lt;/a&gt; &lt;code&gt;-s&lt;/code&gt;, only found bound to noun phrases. Meanwhile &lt;code&gt;cat&lt;/code&gt; is already in its smallest most unpredictable form that conveys meaning; so &lt;code&gt;cat&lt;/code&gt; is a morpheme. The study of morphemes is known as morphology.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Really cool observation&lt;/strong&gt; : It’s helpful to divide words into morphemes because it lets us see patterns across languages. One word in one language might be a part of a word in another. For example, the sentence &lt;code&gt;she is my friend&lt;/code&gt; in English versus the sentence &lt;code&gt;elle est mon amie&lt;/code&gt; in French. You can see that French adds a feminine suffix morpheme since French nouns have gender. Also, sometimes in other languages a sentence could be reduced to several words based on the language’s morphology. Looking at morphemes allows one to enjoy and observe the differences and similarities across languages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Free morpheme: morpheme can stand by itself and can appear within lexemes&lt;/li&gt;
&lt;li&gt;Compound morpheme: two or more free morphemes together&lt;/li&gt;
&lt;li&gt;Bound morpheme: morpheme can’t stand by itself&lt;/li&gt;
&lt;li&gt;Root: stand-alone base morpheme 

&lt;ul&gt;
&lt;li&gt;note: not all roots are free morphemes: &lt;code&gt;conceive&lt;/code&gt;, &lt;code&gt;receive&lt;/code&gt;, &lt;code&gt;deceive&lt;/code&gt;. However, all free morphemes are roots.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Affix: morpheme that is attached to a word stem to form a new word or word form 

&lt;ul&gt;
&lt;li&gt;note: &lt;a href="https://en.wikipedia.org/wiki/Cranberry_morpheme"&gt;not all bound morphemes are affixes&lt;/a&gt;, but all affixes are bound morphemes.&lt;/li&gt;
&lt;li&gt;note: there are &lt;a href="https://en.wikipedia.org/wiki/Affix"&gt;many categories of affixes&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fusional morphology comes into play when a single inflectional morpheme denotes multiple grammatical, syntactic, or semantic features. Take my example from before in French, &lt;code&gt;elle est mon amie&lt;/code&gt;. If you just look at the word &lt;code&gt;amie&lt;/code&gt; you can tell it’s both singular and feminine by the feminine suffix morpheme &lt;code&gt;e&lt;/code&gt;. English and French are both &lt;a href="https://en.wikipedia.org/wiki/Fusional_language"&gt;fusional languages&lt;/a&gt;. However, not all languages are like this. By contrast, Classical Chinese has very little morphology, using almost exclusively free morphemes and depending on word order to convey meaning. By analyzing the &lt;a href="https://en.wikipedia.org/wiki/Linguistic_typology"&gt;linguistic topology&lt;/a&gt; of languages we can further classify them.&lt;/p&gt;

&lt;p&gt;We can represent morphological structure graphically in the form of tree diagrams. Each node in the tree diagram would represent a morpheme. This also means that morphological structure is &lt;a href="https://en.wikipedia.org/wiki/Recursion"&gt;recursive&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Abbreviations and conventions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;N = noun&lt;/li&gt;
&lt;li&gt;A = adjective&lt;/li&gt;
&lt;li&gt;V = verb&lt;/li&gt;
&lt;li&gt;P = preposition&lt;/li&gt;
&lt;li&gt;Adv = adverb&lt;/li&gt;
&lt;li&gt;Af = affix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at the tree diagram for the word &lt;code&gt;breathlessness&lt;/code&gt;. If you’d like to mess around creating your own, check out &lt;a href="https://github.com/maxwelldemaio/jssyntaxtree"&gt;this GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ek01sAD_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://maxwelldemaio.github.io/blog/assets/syntax_tree.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ek01sAD_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://maxwelldemaio.github.io/blog/assets/syntax_tree.png" alt="Syntax tree example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The derivational steps assumed are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;[Noun breath] + [Suffix less] = [Adjective breathless]&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;[Adjective breathless] + [Suffix ness] = [Noun breathlessness]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Words are difficult to explain. There are so many edge cases about the word “word” that explaining things in terms of morphemes allows us to be more precise. The study of morphemes is morphology, researching what words really mean.&lt;/p&gt;

&lt;h2&gt;
  
  
  Works Cited
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CrashCourse. “Morphology: Crash Course Linguistics #2” &lt;em&gt;YouTube&lt;/em&gt;, 18 Sept. 2020, &lt;a href="https://youtu.be/93sK4jTGrss"&gt;https://youtu.be/93sK4jTGrss&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;“Morphology.” &lt;em&gt;Wikipedia&lt;/em&gt;, 12 June 2021, &lt;a href="https://en.wikipedia.org/wiki/Morphology"&gt;https://en.wikipedia.org/wiki/Morphology&lt;/a&gt;. Accessed 4 July 2021.&lt;/p&gt;

</description>
      <category>linguistics</category>
      <category>language</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
