Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

final class without implementing interfaces can be creating by Spring

This is a final class without implementing interfaces:

@Component("finalClass")
public final class FinalClass {
    public String hello(){
        return "hello";
    }
}

This is the test class:

 public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        FinalClass finalClass = (FinalClass) ac.getBean("finalClass");
        System.out.println(finalClass.hello());
    }
}

It can run. The final class can not be extending, and it doesn't implement any interfaces. Why can Spring create it?

like image 672
Changchao Liu Avatar asked May 25 '26 10:05

Changchao Liu


1 Answers

It's because spring uses reflection to do that, which can overcome those limitations. That's why spring can create proxies over objects, that are final, reach to private members and so on.

like image 138
Andronicus Avatar answered May 27 '26 23:05

Andronicus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!