Reputation: 33
I'm creating a service using annotation:
@Service
@Repository
public class UserServiceImpl implements UserService {
private String defaultPassword;
@Autowired
private UserRepository userRepository;
// ...
@Override
public void setDefaultPassword(String defaultPassword) {
this.defaultPassword = defaultPassword;
}
@Override
public String getDefaultPassword() {
return defaultPassword;
}
}
I want to configure defaultPassword
value using Spring XML configuration not via annotation, so I added the following in XML definition:
<bean id="userServiceImpl" class="com.test.service.UserServiceImpl">
<property name="defaultPassword" value="youmustchangethis" />
</bean>
Then, I write test like:
public class UserServiceImplTest extends AbstractServiceImplTest {
@Autowired
private UserService userService;
// ...
}
I've matched the bean name in annotation and XML declaration (using bean id). My question is will userService
variable in UserServiceImplTest
always injected by the same singleton bean that configured in both XML and annotation? I've searched the documentation but didn't find explanation for 'hybrid' approach like this.
Upvotes: 3
Views: 4300
Reputation: 12870
In this case bean defined in xml file will overwrite bean generated from annotation. That is because spring names annotation beans based on the class name with the first letter changed into lowercase. Unless you give it an explit name. There cannot exist two beans of the same name so xml definition overrides the annotation one.
You end up with one singleton called userServiceImpl
here.
Upvotes: 4