通过springframework注解,设置属性值和调用方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.msb.controller" ></context:component-scan>
</beans>
package com.msb.controller.service.impl;
import com.msb.controller.dao.Impl.UserDaoImplA;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@NoArgsConstructor
@Data
@Service
@AllArgsConstructor
//@Autowired只要写一次对应的扫描包下的实体类都进行自动加载其属性
public class UserServiceImpl implements UerService {
@Autowired
/*@Qualifier("UserDaoImplA")这里可以指定加载谁的属性,如不指定全部加载*/
@Value("小明")
private String name;
@Value("男")
private String gender;
@Value("12")
public Integer age;
@Override
public void add() {
System.out.println("UserServiceImpl");
}
}
//这是一个接口,多个类共调一个接口
package com.msb.controller.service.impl;
import org.springframework.stereotype.Service;
@Service
public interface UerService {
void add();
}
package com.msb.controller.dao.Impl;
import com.msb.controller.dao.UserDao;
import com.msb.controller.service.impl.UerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImplA implements UerService {
private String name;
@Override
public void add() {
System.out.println("userFirstImplA");
}
}
package com.msb.controller.dao.Impl;
import com.msb.controller.dao.UserDao;
import com.msb.controller.service.impl.UerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImplB implements UerService {
@Value("UserDaoImplB")
private String name;
@Override
public void add() {
System.out.println("userFirstImplB");
}
}