fashuser
fashuser

Reputation: 1479

How to autowired bean of spring class

I can have create bean with explicit bean factory method.

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();
        }

}

My goal is avoid factory method.

Upvotes: 2

Views: 155

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

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.

Upvotes: 1

Related Questions