Giter Site home page Giter Site logo

overriding-autowired's Introduction

What is this?

This is a bare-bones Spring 4 example demonstrating how @Autowired works when the dependency is explicitly wired.

How does @Autowired work when the dependency is explicitly wired, anyway?

So glad you asked.

If you annotate a method as @Autowired

and you have the requisite application of aspects happening

then Spring will attempt to fulfill that dependency from available beans, by default matching by type, and failing if an ambiguity arises.

However, if you explicitly fulfill the dependency

then @Autowired has no effect.

In particular, in this case, Spring doesn't attempt to auto-wire the dependency by type and then also subsequently explicitly fulfill the dependency.

Why does this matter?

This nuance to how @Autowired works is key to being able to configure your way out of situations where @Autowired would have failed for ambiguity where that ambiguity is resolved by your explicit configuration.

How does this example code demonstrate this

The example involves a HairSplitter object that depends upon a Knife.

public class HairSplitter {
    ...

    @Autowired
    public void setKnife( Knife aKnife ) {
        this.knife = aKnife;
    }
}

and an applicationContext.xml that defines two Knife instances (such that autowiring by type would fail for ambiguity).

<beans ... ">

    <context:annotation-config />

    <bean id="barberKnife" class="io.ghost.apetro.example.BarberKnife" />

    <bean id="butterKnife" class="io.ghost.apetro.example.ButterKnife" />

    <bean id="hairSplitter" class="io.ghost.apetro.example.HairSplitter" >
      <property name="knife" ref="barberKnife" />
    </bean>

</beans>

Since the applicationContext.xml explicitly declares how to fulfill HairSplitter's dependency on Knife, @Autowired doesn't fail for ambiguity and instead that hairSplitter.setKnife() is called just once, with the barberKnife.

That setKnife() is called just once and not twice is observable from what the test method prints to the console

setKnife(io.ghost.apetro.example.BarberKnife@7a096dab)

and more convincingly by the assertion on a counter method in that test:

public class DemonstrateOverridingAutowireTest {

    /**
     * This is the demonstration.
     */
    @Test
    public void testOverridingAutowire() {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        HairSplitter hairSplitter = (HairSplitter) context.getBean("hairSplitter");

        // Even though setKnife() is annotated as Autowired, only the explicit injection from the applicationContext.xml
        // file will fire, so the setter method will have been called only once.
        assertEquals(1, hairSplitter.getNumberOfTimesSetKnifeCalled());
    }


How do I run this code myself?

Option 1: use your IDE

Use your IDE of choice to run the single JUnit @Test in io.ghost.apetro.example.DemonstrateOverridingAutowireTest.

Option 2: use Maven

     mvn test

overriding-autowired's People

Contributors

apetro avatar renovate-bot avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

renovate-tests

overriding-autowired's Issues

[DepShield] (CVSS 7.5) Vulnerability due to usage of org.springframework:spring-core:4.0.5.RELEASE

Vulnerabilities

DepShield reports that this application's usage of org.springframework:spring-core:4.0.5.RELEASE results in the following vulnerability(s):


Occurrences

org.springframework:spring-core:4.0.5.RELEASE is a transitive dependency introduced by the following direct dependency(s):

org.springframework:spring-core:4.0.5.RELEASE

org.springframework:spring-beans:4.0.5.RELEASE
        └─ org.springframework:spring-core:4.0.5.RELEASE

org.springframework:spring-context:4.0.5.RELEASE
        └─ org.springframework:spring-aop:4.0.5.RELEASE
              └─ org.springframework:spring-core:4.0.5.RELEASE
        └─ org.springframework:spring-expression:4.0.5.RELEASE
              └─ org.springframework:spring-core:4.0.5.RELEASE
        └─ org.springframework:spring-core:4.0.5.RELEASE

This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

maven
pom.xml
  • org.springframework:spring-core 4.3.22.RELEASE
  • org.springframework:spring-beans 4.3.22.RELEASE
  • org.springframework:spring-context 4.3.22.RELEASE
  • junit:junit 4.12

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.