Spring 基于xml装配bean和使用注解实现依赖注入

基于xml装配bean

实例化方式

1. 默认构造

格式:

<bean id="" class="">

实体类:

public class UserServiceImpl  implements UserSevice{
    @Override
    public void addUser() {
        System.out.println("Adding!!");
    }
}

示例:

// xml
<bean id="userServiceImpl" class="com.zamao.inter.UserServiceImpl"></bean>
// class
public class UserServiceImpl  implements UserSevice{

    @Override
    public void addUser() {
        System.out.println("Adding!!");
    }
}
// test
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/zamao/inter/applicationContext.xml");
UserServiceImpl userService = applicationContext.getBean("userServiceImpl",UserServiceImpl.class);
userService.addUser();

2. 静态工厂

格式:

<bean id="" class="工厂类" factory-method="静态方法"> 

实体类:

public class UserServiceImpl  implements StaticuserSevice {
    @Override
    public void addUser() {
        System.out.println("Adding!! static Factory");
    }
}

工厂类:

public class Factory {
	//静态方法生产bean
    public static StaticuserSevice createUserService ()
    {
        return new UserServiceImpl();
    }
}

xml:

<bean id="userServiceImpl" class="com.zamao.staticfactory.Factory" factory-method="createUserService"></bean>

测试:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext
            ("com/zamao/staticfactory/applicationContext.xml");
StaticuserSevice userSevice =applicationContext.getBean("userServiceImpl",StaticuserSevice.class);
userSevice.addUser();

3. 实例工厂

格式:

 <bean id="" factory-bean="工厂类id" factory-method="工厂方法"> 

实体类:

public class SimpleuserServiceImpl implements  SimpleuserSevice{
    @Override
    public void addUser() {
        System.out.println("Adding!! simple Factory");
    }
}

工厂类:

public class Factory {
	// 普通方法生产bean
    public  SimpleuserServiceImpl createUserService ()
    {
        return new SimpleuserServiceImpl();
    }
}

xml:

    <bean id= "factory" class="com.zamao.simplefactory.Factory"></bean>
    <bean id="simpleUserServiceImpl" factory-method="createUserService" factory-bean="factory" ></bean>

测试:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext
            ("com/zamao/simplefactory/applicationContext.xml");
SimpleuserSevice simpleuserSevice =applicationContext.getBean("simpleUserServiceImpl", SimpleuserSevice.class);
simpleuserSevice.addUser();

属性注入

1. 构造方法注入

实体类:

public class User {
    String name;
    Integer age;
    Address address;

    public User(String name, Integer age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}
public class Address {
    String name;

    public Address(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Address{" +
                "name='" + name + '\'' +
                '}';
    }
}

xml:

<bean id="address" class="com.zamao.constructor.Address">
    <constructor-arg value="shandong"></constructor-arg>
</bean>
<bean id="user" class="com.zamao.constructor.User">
    <constructor-arg value="zs"></constructor-arg>
    <constructor-arg value="12"></constructor-arg>
    <constructor-arg ref="address"></constructor-arg>
</bean>

测试:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/zamao/constructor/applicationContext.xml");
User user =  applicationContext.getBean("user",User.class);
System.out.println(user.toString());

2. setter 方法注入

格式:

 <bean><property name="" value|ref=""> </property><bean>

实体类与上面相同,只不过将构造方法换成了setter方法
xml:

<bean id="address" class="com.zamao.Setter.constructor.Address">
   <property name="name" value="shandong"></property>
</bean>
<bean id="user" class="com.zamao.Setter.constructor.User">
   <property name="name" value="zs"></property>
   <property name="age" value="21"></property>
   <property name="address" ref="address"></property>
</bean>

3. p 命名空间

格式:

p 命名空间: 简化property <bean p:属性名="普通值" p:属性名-ref="引用值">  注意声明命名空间

xml:

// 声明p命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="address" class="com.zamao.pcontext.constructor.Address" p:name="shandong">
</bean>
<bean id="user" class="com.zamao.pcontext.constructor.User" p:name="zs" p:age="21" p:address-ref="address">
</bean>

基于注解实现依赖注入

注解 : 就是一个类,使用@注解名称

开发中,使用注解取代xml配置文件

@Component 取代< bean class="">

@Component(“id”) 取代< bean id="" class="">
web 开发,提供3个@Component 注解衍生注解

@Repository: dao 层

@ Service : service 层

@ Controller : web 层

注入的方式如下:
普通值 : @Value("");

引用值 :

  • 方式1:按照[类型]注入:

    • @Autowired
  • 方式2: 按照[名称]注入

    • @Autowired

      @Qualifier(“名称”)

    • @Resource(“名称”)

注意:
通过注解装配bean需要在xml文件中配置需要扫描的包。
在xml中添加如下代码:

<context:component-scan base-package="com.zamao.annotation"></context:component-scan>

这里通过基于注解装配bean实现三层架构来进行举例

Dao 层:

@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao{
    @Override
    public void saveUser() {
        System.out.println("Save");
    }
}

Service层:

@Service
public class UserServiceImpl implements UserSevice {
    UserDao userDao;
    @Autowired
    @Qualifier("userDaoImpl")
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void addUser() {
        userDao.saveUser();
    }
}

Controller层:

@Controller("userControllerImpl")
public class UserControllerImpl implements UserController {

    @Autowired
    UserSevice userSevice;
    @Value("jacw")
    String name;
    @Override
    public void insertUser() {
        userSevice.addUser();
    }
    public String toString()
    {
        return name;
    }
}

Service层通过按照[名称]的方式注入Dao,Controller通过按照[类型]的方式注入Service。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值