3

我可以使用显式 bean 工厂方法创建 bean。

package org.package;

import org.springframework.security.web.PortResolver;
import org.springframework.security.web.PortResolverImpl;


@Configuration
public Configuration {

    @Bean
        public PortResolver portResolver(){
            return new PortResolverImpl();
        }

}

我的目标是避免工厂方法。

4

1 回答 1

1

If you don't want a factory method, let Spring instantiate an instance of your class for you by annotating your class with @Component and make your @Configuration class @ComponentScan its package.

When Spring scans that package, it will find your class, use its default constructor (or constructor annotated with @Inject or @Autowired) and use it to make an instance of your class. Spring will then add that instance to its context.

Because PortResolveImpl is not under your control, you will need to use a @Bean factory method.

于 2013-07-10T18:55:42.783 回答