{
    "content": "<h1>Setter Injection<\/h1><p><a href=\"..\/Setter-Injection\/\">Setter-Injection<\/a> is a structural design pattern used in <a href=\"..\/Software-Engineering\/\">Software-Engineering<\/a> to implement <a href=\"..\/Dependency-Injection\/\">Dependency-Injection<\/a>. It involves providing the required dependencies of a class through public setter methods after the object has been instantiated. This approach is a core mechanism of the <a href=\"..\/Inversion-of-Control\/\">Inversion-of-Control<\/a> principle, which is widely utilized in enterprise frameworks such as the <a href=\"..\/Spring-Framework\/\">Spring-Framework<\/a> and <a href=\"..\/Google-Guice\/\">Google-Guice<\/a>.<\/p><p>The primary advantage of <a href=\"..\/Setter-Injection\/\">Setter-Injection<\/a> is its flexibility. Since dependencies are assigned after the object is created, it allows for optional dependencies and the ability to re-configure an object at runtime without needing to create a new instance. This is particularly useful in <a href=\"..\/Unit-Testing\/\">Unit-Testing<\/a>, where <a href=\"..\/Mock-Objects\/\">Mock-Objects<\/a> can be easily injected into the class being tested. However, a notable drawback is that it can leave an object in an uninitialized or inconsistent state if the <a href=\"..\/IoC-Container\/\">IoC-Container<\/a> fails to call a required setter method, which can lead to a <a href=\"..\/NullPointerException\/\">NullPointerException<\/a> at runtime.<\/p><p>In <a href=\"..\/Java\/\">Java<\/a> development, <a href=\"..\/Setter-Injection\/\">Setter-Injection<\/a> is frequently contrasted with <a href=\"..\/Constructor-Injection\/\">Constructor-Injection<\/a>. While <a href=\"..\/Constructor-Injection\/\">Constructor-Injection<\/a> promotes <a href=\"..\/Immutability\/\">Immutability<\/a> and ensures that all mandatory dependencies are satisfied at creation time, <a href=\"..\/Setter-Injection\/\">Setter-Injection<\/a> is preferred for optional dependencies or for resolving circular dependencies that constructors cannot handle. For further technical details, developers can refer to the <a href=\"https:\/\/docs.spring.io\/spring-framework\/reference\/core\/beans\/dependencies\/factory-collaborators.html#beans-setter-injection\">Spring Reference Documentation<\/a> or read <a href=\"https:\/\/martinfowler.com\/articles\/injection.html\">Martin-Fowler<\/a>'s seminal article on the subject.<\/p><ul><li><a href=\"..\/Constructor-Injection\/\">Constructor-Injection<\/a><\/li><li><a href=\"..\/Inversion-of-Control\/\">Inversion-of-Control<\/a><\/li><li><a href=\"..\/Dependency-Injection\/\">Dependency-Injection<\/a><\/li><li><a href=\"..\/Software-Design-Patterns\/\">Software-Design-Patterns<\/a><\/li><\/ul>",
    "tags": [
        "setter injection",
        "dependency injection",
        "inversion of control",
        "spring framework",
        "java",
        "software architecture",
        "design patterns",
        "oop",
        "backend development",
        "software engineering"
    ]
}