使用@Autowired注解进行Spring依赖注入,而无需setter方法。

18 浏览
0 Comments

使用@Autowired注解进行Spring依赖注入,而无需setter方法。

我从几个月前开始使用Spring,我以为使用@Autowired注解进行依赖注入也需要一个setter方法来注入字段。\n所以,我以这种方式使用它:\n

@Controller
public class MyController {
    @Autowired
    MyService injectedService;
    public void setMyService(MyService injectedService) {
        this.injectedService = injectedService;
    }
    ...

\n}\n但是今天我尝试了这个:\n

@Controller
public class MyController {
    @Autowired
    MyService injectedService;
    ...

\n}\n令人惊讶的是,没有编译错误,没有启动错误,应用程序完美运行...\n所以我的问题是,使用@Autowired注解进行依赖注入是否需要setter方法?\n我使用的是Spring 3.1.1版本。

0
0 Comments

在上述代码中,存在一个名为Test的类,其中包含一个名为test2的私有成员变量以及一个名为getTest2的公有方法。Test2类也存在,其中包含一个名为i的私有成员变量以及一个名为getI的公有方法。另外,还有一个名为TestReflection的类,其中的main方法使用反射来实例化Test类,并通过反射设置test2成员变量的值。

问题的出现原因是在TestReflection类的main方法中,使用了反射来设置test2成员变量的值。这样做的结果是,Test类的构造函数不会被调用,因此test2成员变量不会被初始化。

为了解决这个问题,可以使用Spring的依赖注入功能,并使用@Autowired注解来自动注入test2成员变量的值。具体步骤如下:

1. 首先,需要在Test类中添加@Autowired注解,将其应用于test2成员变量上。

2. 然后,在TestReflection类的main方法中,使用Spring的ApplicationContext来获取Test类的实例。

3. 最后,通过ApplicationContext对象来获取Test类的实例,并让Spring自动注入test2成员变量的值。

以下是修改后的代码示例:

package com.techighost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    @Autowired
    private Test2 test2;
    public Test() {
        System.out.println("Test constructor called");
    }
    public Test2 getTest2() {
        return test2;
    }
}
public class TestReflection {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Test test = context.getBean(Test.class);
        Test2 test2 = test.getTest2();
        System.out.println("i=" + test2.getI());
    }
}

在上述修改后的代码中,使用了Spring的ApplicationContext和ClassPathXmlApplicationContext来获取Test类的实例,并让Spring自动注入test2成员变量的值。这样,test2成员变量将会被正确初始化,并且输出结果将会是“i=5”。

通过以上的修改和解释,我们解决了使用@Autowired注解实现Spring依赖注入而不使用setter方法的问题。

0
0 Comments

如果Java安全策略允许Spring更改对于受包保护字段的访问权限,则不需要setter方法。

通常情况下,Spring通过@Autowired注解来实现依赖注入。依赖注入是一种通过将依赖对象注入到需要它们的对象中来解耦和简化代码的方式。

在使用@Autowired注解进行依赖注入时,通常需要一个setter方法来设置依赖对象。然而,如果Java安全策略允许Spring更改对于受包保护字段的访问权限,就不需要使用setter方法。

通过@Autowired注解,Spring可以直接访问受包保护字段,并将依赖对象注入到其中。这样就不需要显式地调用setter方法来设置依赖对象。

这种方式简化了代码,减少了不必要的setter方法的编写。同时,由于依赖对象是通过注解方式注入的,代码的可读性和可维护性也得到了提高。

总结起来,当Java安全策略允许Spring更改对于受包保护字段的访问权限时,可以使用@Autowired注解进行依赖注入,而无需使用setter方法。这种方式简化了代码,提高了代码的可读性和可维护性。

0
0 Comments

Spring的依赖注入是通过反射机制来完成的,因此不需要使用setter方法来设置值。即使字段是私有的,Spring的@Autowired注解也可以正常工作,无需使用setter方法。

具体原因和解决方法可以参考这篇文章:How does Spring work

代码示例:

public class MyClass {
    @Autowired
    private MyDependency myDependency;
    
    public void doSomething() {
        myDependency.doSomething();
    }
}
public class MyDependency {
    public void doSomething() {
        // do something
    }
}

在上面的示例中,字段`myDependency`使用了@Autowired注解,Spring会自动将其注入到MyClass类中,无需使用setter方法。

Spring的依赖注入可以通过@Autowired注解来实现,而不需要使用setter方法。这种方式使得代码更加简洁和易于维护。同时,字段可以是私有的,不会影响@Autowired注解的使用。

0