Can I use @Profile Tags on Components without creating an @Configuration class in Spring Boot or a Web.xml
<p>I've managed to avoid developing any xml files so far for my Spring Boot Maven project (apart from the pom) with them all being generated on compile and I was hoping to keep this way by defining my profiles within the run commands as specified <a href="http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html#" rel="nofollow">here</a>.</p>

<p>By simply using @<a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html" rel="nofollow">ComponentScan</a> my main class to enable the scanning of components and tagging my DAO as @<a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Repository.html" rel="nofollow">Repository</a> I have successfully managed to autowire my UserDAOmySQLImpl class (which inherits UserDAO).</p>

<pre><code>    @Autowired
    private UserDAO userDAO;
</code></pre>

<p>Then looking forward to add a second DAO for when in Live where we use a db8 implementation I need the application to figure out which UserDAO implementation needs to be used. <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html" rel="nofollow">Profiles</a> seemed to be the answer.</p>

<p>After <a href="http://www.mkyong.com/spring/spring-profiles-example/" rel="nofollow">some reading</a>, it seemed that mostly I need to add in some sort of configuration classes and external xml to manage these profiles and components, though this seems messy and I am hoping unnecessary.</p>

<p>I have tagged by two implementations as so:</p>

<pre><code>@Repository
@Profile("dev")
public class UserDAOmySQLImpl implements UserDAO {...
</code></pre>

<p>-</p>

<pre><code>@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...
</code></pre>

<p>And then set the active profile through the Maven goals as specified <a href="http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html#" rel="nofollow">here</a> in hope that this would be a nice clean solution so I could specify the active profile within the goal for our dev and live build scripts respectively.</p>

<p>My current goal for testing is: </p>

<pre><code>  spring-boot:run -Drun.profiles=dev
</code></pre>

<p>However, when building I receive an error that both beans are being picked up.</p>

<pre><code> *No qualifying bean of type [com.***.studyplanner.integration.UserDAO] is defined: expected single matching bean but found 2: userDAOdb8Impl,userDAOmySQLImpl*
</code></pre>

<p><strong>Questions</strong></p>

<ul>
<li><p>Is the profile set in the Maven Goal the one being checked against when using the @Profile tag?</p></li>
<li><p>Why is Spring picking up both implementations, when if the profile isn't being set properly surely neither implementation should be selected?</p></li>
<li><p>Any suggestions on a nice clean way to achieve what I'm looking for?</p></li>
</ul>

<p><strong>Bonus</strong></p>

<p>I would really like to set the profile within the app, as it would be easy to simply check whether an environment file exists to decide which profile to use, though I'm struggling to replicate this (found within <a href="http://www.mkyong.com/spring/spring-profiles-example/" rel="nofollow">Spring Profiles Examples</a></p>

<pre><code>public static void main(String[] args) {

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
      //Enable a "live" profile
      context.getEnvironment().setActiveProfiles("live");
      context.register(AppConfig.class);
      context.refresh();        
      ((ConfigurableApplicationContext) context).close();
    }
</code></pre>

<p>in to the unusual main class in the application I am working on which I am hesitant to play around with.</p>

<pre><code> public class StudyPlannerApplication {
     ...
     public static void main(String[] args) {
            SpringApplication.run(StudyPlannerApplication.class, args);
         }
     ...
 }
</code></pre>

<p>Any help would much appreciated.</p>

<p>Cheers,
Scott.</p>